Page 1 of 1

HttpClient in C# and HTTPRequest in PB

Posted: Sun Sep 08, 2024 4:57 pm
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;

Re: HttpClient in C# and HTTPRequest in PB

Posted: Sun Sep 08, 2024 6:24 pm
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?

Re: HttpClient in C# and HTTPRequest in PB

Posted: Sun Sep 08, 2024 6:33 pm
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

Re: HttpClient in C# and HTTPRequest in PB

Posted: Sun Sep 08, 2024 8:04 pm
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$

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 9:13 am
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?

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 9:20 am
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.

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 9:45 am
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

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 10:24 am
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$

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 11:09 am
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}

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 11:37 am
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())

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 11:40 am
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"
}

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 12:12 pm
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}

Re: HttpClient in C# and HTTPRequest in PB

Posted: Mon Sep 09, 2024 5:28 pm
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.