Page 1 of 1

How to Send HTTP POST request

Posted: Sun Apr 23, 2017 12:35 pm
by plouf
Hi trying to send a simple POST request with some custom headers AND some data

code used

Code: Select all

  Procedure HttpGetEvent(Success, Result$, UserData)
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
      NewMap Headers.s()
      Headers.s("myAuth") = "12345"
      Headers.s("x-customvalue") = "10"

      HTTPRequest(#PB_HTTP_Get, "http://192.168.1.199/config/1", "", @HttpGetEvent(), 0, Headers())
from Debug window outpute i get the following message -> HTTPRequest(): Error
however it send followng data (OPTIONS instead of POST) and NO headers

Code: Select all

http://192.168.1.199/conf/1

OPTIONS /conf/1 HTTP/1.1
Host: 192.168.1.199
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: el-GR,el;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: myAuth,x-customvalue
Origin: http://127.0.0.1:9080
Connection: keep-alive

HTTP/1.1 400 Bad Request
Content-Type: text/html
Content-Length: 0
Connection: close
AuthInfo: 

Re: How to Send HTTP POST request

Posted: Sun Apr 23, 2017 6:06 pm
by tj1010
http://forums.spiderbasic.com/viewtopic ... 3884#p3883

Yeah the doc is kind of lacking on pointing out "Parameters" are POST body on POST. Hit F1 with cursor on a function to open it's doc.

Re: How to Send HTTP POST request

Posted: Sun Apr 23, 2017 10:06 pm
by plouf
yet again POST has wrong form, or i am doing something wrong

i want to send actual headers of POST, actually i want to send Basic Authentication in POST. but Headers in HTTPrequest seems to be something different

if i use Headers() request changes frm POST to OPTIONS ! (?) and offcourse authentication headers gos to custom headers !

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 8:21 am
by Num3
HTTPRequest(#PB_HTTP_Get, "http://192.168.1.199/config/1", "", @HttpGetEvent(), 0, Headers())

HTTPRequest(#PB_HTTP_Post, "http://192.168.1.199/config/1", "YOUR_POST_DATA_GOES_HERE", @HttpGetEvent(), 0, Headers())

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 9:39 am
by SparrowhawkMMU
plouf wrote:if i use Headers() request changes frm POST to OPTIONS ! (?) and offcourse authentication headers gos to custom headers !
This is normal behaviour and is instantiated by your browser as part of the protocol when modifying a POST (or GET, or HEAD although SB does not currently support this) request with a cross origin request that includes a header that is not in the CORS-safelisted request-header list.

See this article for an explanation: https://developer.mozilla.org/en-US/doc ... ntrol_CORS

To summarise from the article:
the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method
Your backend will need to check for the HTTP verb and if OPTIONS, validate those headers and respond. There is no need to then explicitely resend the request - it is all handled for you.

There are various articles online on how to handle this - for example in PHP: https://www.dinochiesa.net/?p=754

HTH.

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 9:40 am
by SparrowhawkMMU
@fred - these queries around HTTP requests (especially the access allow origin server header and CORS generally) pop up all the time. It is probably worth adding a link to the Mozilla Dev Network article above into the SB help page for HTTPRequest() ?

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 1:10 pm
by plouf
so you tell me that because i send it through browser request change

therefore i should alter my original question in "How to send POST with basic authentication + some content"

p.s. mainly interest in android app dont know if changed anything this (i have not test yet in android only browser up to now)

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 2:08 pm
by SparrowhawkMMU
Not quite - the request changes because you are using a non-listed header as part of a CORS request.

CORS means that the browser (the Android app wrapped in Cordova) is making a call to a resource in another domain. The header(s) you have added means that the request changes to a pre-flight check request instead of being a normal request.

So the client (Andoird) sends POST. But because you have added the header, it first sends OPTIONS. Your API (server) must check that the list of headers being sent is valid, and respond to the client by sending the valid headers, at which point the client will send the actual POST data.

It took me a while to figure it out when I first encountered it, but it's not too complicated - you can follow the PHP example in that second link to see what needs sending back. there are many more online for the various common server languages (python, ruby, node.js etc)

Re: How to Send HTTP POST request

Posted: Mon Apr 24, 2017 6:32 pm
by plouf
however i can not change server .
wondering if i could access some android api and from there send my request !

Re: How to Send HTTP POST request

Posted: Tue Apr 25, 2017 11:16 am
by SparrowhawkMMU
Possibly - I do not know Android or Cordova well at all so I cannot help you there, sorry. Good luck - I hope you find a solution. :)

Just a thought: if you have your own server, you could pass the request to that, then make a call from there to the target server, and route the response back to your Android app?