HttpClient in C# and HTTPRequest in PB

Just starting out? Need help? Post your questions and find answers here.
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

HttpClient in C# and HTTPRequest in PB

Post by punak »

hi all
i need help to convert this C# codes to pb, Please help me in a short time, I really need it.

Code: Select all

var client = new HttpClient();
string[] receiver = { "mobilenumber"};
var data = new
{
Message = "message",
SenderNumber = "sendermobilenumber",
MobileNumber = receiver,
SendToBlocksNumber = true
};
var url = "https://api.smspanel.com/api/sendsms";
client.DefaultRequestHeaders.Add("ApiKey", "myaccesscode");
var objectStr = JsonConvert.SerializeObject(data);
var content = new StringContent(objectStr, Encoding.UTF8, "application/json");
var response = client.PostAsync(url, content).Result;
string resultContent = await response.Content.ReadAsStringAsync();
return resultContent;
Last edited by punak on Sun Sep 08, 2024 6:27 pm, edited 1 time in total.
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

Re: HttpClient in C# and HTTPRequest in PB

Post by punak »

what is the format of writing the data parameter in the HTTPRequest function?

Code: Select all

{
Message = "message",
SenderNumber = "sendermobilenumber",
MobileNumber = receiver,
SendToBlocksNumber = true
};
Just someone tell me how to send these data to the data parameter of HTTPRequest . Is this a hard job?
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: HttpClient in C# and HTTPRequest in PB

Post by spikey »

It needs to look something like this but I can't test this so no guarantees. You need to create a JSON object and add the fields to it. Then you need to use ComposeJSON to turn the complete structure back into a string. (Or build a string that looks the same - you don't need a JSON object). This can be supplied to the Data parameter of HTTPRequest.

Code: Select all

NewMap Header$()
Header$("Content-Type") = "application/json"
Header$("ApiKey") = "myaccesscode"

If CreateJSON(0)
  Message = SetJSONObject(JSONValue(0))
  SetJSONString(AddJSONMember(Message, "Message"), "message")
  SetJSONString(AddJSONMember(Message, "SenderNumber"), "sendermobilenumber")
  SetJSONString(AddJSONMember(Message, "MobileNumber"), "receivermobilenumber")
  SetJSONBoolean(AddJSONMember(Message, "SendToBlocksNumber"), #True)
EndIf

HttpRequest = HTTPRequest(#PB_HTTP_Post, "https://api.smspanel.com/api/sendsms", ComposeJSON(0), 0, Header$())
If HttpRequest
  Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
  Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
  
  FinishHTTP(HTTPRequest)
Else
  Debug "Request creation failed"
EndIf
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HttpClient in C# and HTTPRequest in PB

Post by infratec »

Or:

Code: Select all

Post$ = ~"{"
Post$ + ~"\"Message\": \"message\","
Post$ + ~"\"SenderNumber\": \"sendermobilenumber\","
Post$ + ~"\"MobileNumber\": \"receiver\","
Post$ + ~"\"SendToBlocksNumber\": true"
Post$ + ~"}"

Debug Post$
And use Post$ as Data$
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

Re: HttpClient in C# and HTTPRequest in PB

Post by punak »

Hello and thank you all for the answers, I really appreciate it.
The problem I have is that the MobileNumber argument takes the string address and this causes an error
Please see the description of the arguments and the example code

SenderNumber string
Message string
MobileNumber [ ] string
SendToBlocksNumber boolean

C#

Code: Select all

string[] receiver = { "mobilenumber"};
var data = new
{
MobileNumber = receiver,
};

Python

Code: Select all

import requests
url = 'https://api.smspanel.com/api/sendsms'
receiver = ["mobiles"]
myobj = {'Message' :'msg',
'SenderNumber' : 'sendermobile',
'MobileNumber' : receiver}
x = requests.post(url, json = myobj, headers = {"ApiKey": "accesscode"})
print(x.text)
how should I write this part in pb?
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HttpClient in C# and HTTPRequest in PB

Post by infratec »

Maybe something like this:

Code: Select all

Post$ = ~"{"
Post$ + ~"\"Message\": \"message\","
Post$ + ~"\"SenderNumber\": \"sendermobilenumber\","
Post$ + ~"\"MobileNumber\": [\"123\", \"456\"],"
Post$ + ~"\"SendToBlocksNumber\": true"
Post$ + ~"}"

Debug Post$
In general: you dn't need a C code, you need the structure/json of this API.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: HttpClient in C# and HTTPRequest in PB

Post by TI-994A »

punak wrote: Sun Sep 08, 2024 6:24 pmJust someone tell me how to send these data to the data parameter of HTTPRequest . Is this a hard job?
Hi @punak. It's quite simple, really. Perhaps seeing a live working example, along with the actual API script, might be helpful. :D

> PureBasic and REST APIs - An HTTPRequest() Example
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
punak
User
User
Posts: 81
Joined: Tue Sep 07, 2021 12:08 pm

Re: HttpClient in C# and HTTPRequest in PB

Post by punak »

@ infratec Thank you very much for your help. :D :D :D :D
only I want the "message" and "mobilenumber" to be read from pb variable. No matter what I did, I could not create them. :(
Please guide me on the same thing

I wrote like this, but it doesn't work

Code: Select all

Post$ = ~"{"
Post$ + ~"\"Message\": \""+message+"\","
Post$ + ~"\"SenderNumber\": \"sendermobilenumber\","
Post$ + ~"\"MobileNumber\": [\""+mobile+"\""],"
Post$ + ~"\"SendToBlocksNumber\": true"
Post$ + ~"}"

Debug Post$
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: HttpClient in C# and HTTPRequest in PB

Post by TI-994A »

punak wrote: Mon Sep 09, 2024 10:24 am...I want the "message" and "mobilenumber" to be read from pb variable...

Here are some helper macros to simplify the task:

Code: Select all

#q = #DOUBLEQUOTE$

Macro stringify(key, value)
  #q + key + #q + ":" + #q + value + #q
EndMacro

Macro stringify_v(key, value)
  #q + key + #q + ":" + value
EndMacro

message.s = "Hello, World!"
mobile.s = "123456789"

Post$ = "{"
Post$ + stringify("Message", message) + ","
Post$ + stringify("SenderNumber", "sendermobilenumber") + ","
Post$ + stringify("MobileNumber", "[" + mobile + "]") + ","
Post$ + stringify_v("SendToBlocksNumber", "true")
Post$ + "}"

Debug Post$
It outputs:

Code: Select all

{"Message":"Hello, World!","SenderNumber":"sendermobilenumber","MobileNumber":"[123456789]","SendToBlocksNumber":true}
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: HttpClient in C# and HTTPRequest in PB

Post by TI-994A »

To fully configure the http call, try something like this:

Code: Select all

#http = "https://"
#host = "api.smspanel.com"
#apiURL = "/api/sendsms"
#q = #DOUBLEQUOTE$

Macro stringify(key, value)
  #q + key + #q + ":" + #q + value + #q
EndMacro

Macro stringify_v(key, value)
  #q + key + #q + ":" + value
EndMacro

message.s = "Hello, World!"
mobile.s = "123456789"

query.s = "{"
query + stringify("Message", message) + ","
query + stringify("SenderNumber", "sendermobilenumber") + ","
query + stringify("MobileNumber", "[" + mobile + "]") + ","
query + stringify_v("SendToBlocksNumber", "true")
query + "}"

apiUrl.s = #http + #host + #apiURL
queryLen.s = Str(StringByteLength(query, #PB_UTF8))

NewMap headers.s()
headers("Host") = #host
headers("Connection") = "close"
headers("User-Agent") = "PureBasic Network Client"
headers("Content-Type") = "application/json"
headers("Content-Length") = queryLen  

httpRequest = HTTPRequest(#PB_HTTP_Post, apiURL, query, #Null, headers())
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: HttpClient in C# and HTTPRequest in PB

Post by spikey »

TI-994A wrote: Mon Sep 09, 2024 11:09 am It outputs:

Code: Select all

{"Message":"Hello, World!","SenderNumber":"sendermobilenumber","MobileNumber":"[123456789]","SendToBlocksNumber":true}
I missed that last night. It's expecting an array for "MobileNumber", I'm guessing it's expecting something like:

Code: Select all

{
  "Message"           : "message",
  "MobileNumber"      : [
      "123",
      "456",
      "7890"
    ],
  "SendToBlocksNumber": true,
  "SenderNumber"      : "sendermobilenumber"
}
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: HttpClient in C# and HTTPRequest in PB

Post by TI-994A »

spikey wrote: Mon Sep 09, 2024 11:40 am...It's expecting an array for "MobileNumber", I'm guessing it's expecting something like:

Code: Select all

{
  "Message"           : "message",
  "MobileNumber"      : [
      "123",
      "456",
      "7890"
    ],
  "SendToBlocksNumber": true,
  "SenderNumber"      : "sendermobilenumber"
}

Try this, then:

Code: Select all

#http = "https://"
#host = "api.smspanel.com"
#apiURL = "/api/sendsms"
#q = #DOUBLEQUOTE$

Macro stringify(key, value)
  #q + key + #q + ":" + #q + value + #q
EndMacro

Macro stringify_v(key, value)
  #q + key + #q + ":" + value
EndMacro

Procedure.s stringify_a(key.s, values.s)
  value.s = #q + key + #q + ":["
  For i = 1 To CountString(values, ",") + 1
    value + #q + StringField(values, i, ",") + #q + ","
  Next i
  value = RTrim(value, ",")
  value + "]"
  ProcedureReturn value
EndProcedure

message.s = "Hello, World!"
mobile.s = "123,456,7890"

query.s = "{"
query + stringify("Message", message) + ","
query + stringify("SenderNumber", "sendermobilenumber") + ","
query + stringify_a("MobileNumber", mobile) + ","
query + stringify_v("SendToBlocksNumber", "true")
query + "}"

apiUrl.s = #http + #host + #apiURL
queryLen.s = Str(StringByteLength(query, #PB_UTF8))

NewMap headers.s()
headers("Host") = #host
headers("Connection") = "close"
headers("User-Agent") = "PureBasic Network Client"
headers("Content-Type") = "application/json"
headers("Content-Length") = queryLen  

httpRequest = HTTPRequest(#PB_HTTP_Post, apiURL, query, #Null, headers())

The query output will look like this:

Code: Select all

{"Message":"Hello, World!","SenderNumber":"sendermobilenumber","MobileNumber":["123","456","7890"],"SendToBlocksNumber":true}
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: HttpClient in C# and HTTPRequest in PB

Post by spikey »

@punak: Just for future reference, it is useful to provide a link to API reference documentation in these instances. It makes it easier to give a sensible answer and reduces the chances of mistake through ambiguity.
Post Reply