How does HTTPRequest() format the raw request?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

How does HTTPRequest() format the raw request?

Post by swhite »

Does HTTPRequest() include the POST data the same way Postman does as shown below? The reason I ask is that the request shown below works fine from Postman but fails in Purebasic.

Code: Select all

   Define NewMap Header.s(),lnR.i
   Header("Content-Type") = "application/json"
   lnR = HTTPRequest(#PB_HTTP_Post,"http://***.198.229.***:85/api/priceChange",~"[{\"FuelGrade\":\"200\",\"UnitPrice\":1.559,\"Price\":1.559,\"Date\":\"20221003\",\"Time\":\"09:40\"}]",0,Header())
   Debug HTTPInfo(lnR,#PB_HTTP_StatusCode) ; Returns 0
POST http://***.198.229.***:85/api/priceChange HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Postman-Token: 05801a9f-ac4a-40b0-9dab-70b2dd94ac07
Host: ***.198.229.***:85
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 86

[{"FuelGrade":"200","UnitPrice":1.559,"Price":1.559,"Date":"20221003","Time":"09:40"}]
Simon White
dCipher Computing
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How does HTTPRequest() format the raw request?

Post by infratec »

POST is POST.
This only means that the informational stuff is in the body section.

The only way to see a difference is to use Wireshark and compare the two sended packets.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How does HTTPRequest() format the raw request?

Post by infratec »

The JSON is valid. Checked with an online validator.
If StatusCode returns "0", you can check if there is an ErrorMessage

Code: Select all

Define NewMap Header.s(),lnR.i

OpenConsole()

Post$ = ~"[{\"FuelGrade\":\"200\",\"UnitPrice\":1.559,\"Price\":1.559,\"Date\":\"20221003\",\"Time\":\"09:40\"}]"
Debug Post$

Header("Content-Type") = "application/json"
lnR = HTTPRequest(#PB_HTTP_Post, "http://127.0.0.1:85/api/priceChange", Post$, #PB_HTTP_Debug, Header())
If lnR
  Debug HTTPInfo(lnR,#PB_HTTP_StatusCode) ; Returns 0
  Debug HTTPInfo(lnR,#PB_HTTP_ErrorMessage)
  FinishHTTP(lnR)
EndIf

Input()
Compile it as console program to see the curl debug messages :!:
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: How does HTTPRequest() format the raw request?

Post by infratec »

For testing without wireshark:

Code: Select all

If CreateNetworkServer(0, 85, #PB_Network_TCP, "127.0.0.1")
  
  *Buffer = AllocateMemory(2048)
  
  Repeat
    Select NetworkServerEvent()
      Case #PB_NetworkEvent_None
        Delay(10)
        
      Case #PB_NetworkEvent_Data
        Client = EventClient()
        RcvLen = ReceiveNetworkData(Client, *Buffer, MemorySize(*Buffer))
        If RcvLen
          Debug PeekS(*Buffer, RcvLen, #PB_UTF8|#PB_ByteLength)
          Answer$ = "HTTP/1.1 200 OK" + #CRLF$
          Answer$ + "Content-Length: 0" + #CRLF$
          Answer$ + #CRLF$
          SendNetworkString(Client, Answer$)
        EndIf

    EndSelect
  ForEver
  
EndIf
Post Reply