requestheader

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

requestheader

Alex Betz-3
I am trying to pass a token in the request header of a curl request with no
success.  Not quite sure what is wrong with my code:

local(req) = curl()
#req->url = 'https://httpbin.org/headers'
#req->contentType = 'application/json'
#req->set(CURLOPT_HEADER(), 1)
#req->set(CURLOPT_HTTPHEADER, array('Authorization' = 'Bearer
XXXXXXXXXXXXXXXX'))

#req->postFields = '{
    "elements":[
        {
            "type":"Contact",
            "id":"1",
            "createdAt":"1403034086",
            "depth":"minimal",
            "name":"[hidden email]",
            "updatedAt":"1410193024",
            "emailAddress":"[hidden email]"
        },
        {
            "type":"Contact",
            "id":"2",
            "createdAt":"1403113589",
            "depth":"minimal",
            "name":"[hidden email]",
            "updatedAt":"1403113589",
            "emailAddress":"[hidden email]"
        }
    ],
    "page":1,
    "pageSize":2,
    "total":527
}'

#req->raw
#req->close

Further, is this the correct way to submit a json file to a site? Any help
appreciated
Thanks
Alex

#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>
Reply | Threaded
Open this post in threaded view
|

Re: requestheader

stevepiercy
On 6/1/17 at 4:26 PM, [hidden email] (Alex Betz) pronounced:

>I am trying to pass a token in the request header of a curl request with no
>success.  Not quite sure what is wrong with my code:
>
>local(req) = curl()
>#req->url = 'https://httpbin.org/headers'
>#req->contentType = 'application/json'

I would defer this Content-Type header.  See below.

>#req->set(CURLOPT_HEADER(), 1)

Not sure this is needed.

>#req->set(CURLOPT_HTTPHEADER, array('Authorization' = 'Bearer
>XXXXXXXXXXXXXXXX'))

Try:

     #req -> set(CURLOPT_HTTPHEADER, staticarray('Authorization'
= 'Bearer XXXXXXXXXXXXXXXX'))

I prefer to build up a Lasso array of pairs, then cast it to a staticarray:

     local(headers) = array(
         'Accept: */*',
         'Content-Type: application/json',
         ('Authorization: Bearer ' + #access_token))

     #req -> set(CURLOPT_HTTPHEADER, #headers -> asstaticarray)

>#req->postFields = '{
>"elements":[
>{
>"type":"Contact",
>"id":"1",
>"createdAt":"1403034086",
>"depth":"minimal",
>"name":"[hidden email]",
>"updatedAt":"1410193024",
>"emailAddress":"[hidden email]"
>},
>{
>"type":"Contact",
>"id":"2",
>"createdAt":"1403113589",
>"depth":"minimal",
>"name":"[hidden email]",
>"updatedAt":"1403113589",
>"emailAddress":"[hidden email]"
>}
>],
>"page":1,
>"pageSize":2,
>"total":527
>}'
>#req->raw
>#req->close

Although you can build a string representation of a JSON object,
I would build a Lasso object instead, inserting Lasso objects
into Lasso objects, then finally json_encode it:

     #req -> set(CURLOPT_POSTFIELDS, json_encode(#postdata))

Finally, for debugging, this is very useful:

     #debug ? #request -> set(CURLOPT_VERBOSE, 1)

...along with the curl docs, when the Lasso docs are insufficient:

https://curl.haxx.se/libcurl/c/easy_setopt_options.html

--steve

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Steve Piercy              Website Builder              Soquel, CA
<[hidden email]>               <http://www.stevepiercy.com/>


#############################################################

This message is sent to you because you are subscribed to
  the mailing list Lasso [hidden email]
Official list archives available at http://www.lassotalk.com
To unsubscribe, E-mail to: <[hidden email]>
Send administrative queries to  <[hidden email]>