[RexxLA] HTTP Post with rexxcurl

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

[RexxLA] HTTP Post with rexxcurl

sahananda
Hi all,

I'm trying to add a member to a mailchimp list using RexxCurl.

The mailchimp api docs give this sample curl call

Curl \

--request POST \

--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \

--user 'anystring:apikey' \

--header 'content-type: application/json' \

--data '{"email_address":"[hidden email]", "status":"subscribed"}' \

--include

My problem seems to be including my data.
I have a PHP sample from stackoverflow which includes the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

and curlopt_postfields does seem to be the way to go, however, the rexxcurl wrapper for CURLOPT_POSTFIELDS is HTTPPOSTFIELDS and the docs say

The passed parameter is a Rexx array, with each item in the array a name/value pair. eg field.1 = 'email=[hidden email]' and field.0 is the number items in the array.

I can set the field to data like this

   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'

   data.0 = 1
   data.1 = 'data='||json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

or I can ignore Mark's instruction like this

   data.0 = 1
   data.1 = json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

but in either case I get a return from the server which I believe means that no data is found:

,"title":"Invalid Resource"
,"status":400
,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"5d65323d-ce8e-4f23-9bbe-3f91279ef563"
,"errors":[{"field":"","message":"Schema describes object, NULL found instead"}]
}

Any suggestions?  I tried using HTTPPOSTDATA but the server did not recognise the request.

thanks Jon

_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

Re: [RexxLA] HTTP Post with rexxcurl

sahananda
I should add, when I try to use HTTPPOSTDATA the response is 

HTTP/1.1 411 Length Required
Your browser sent a request that this server could not understand.<P>
Reference&#32;&#35;7&#46;7367a5c&#46;1530254748&#46;2b0e18

thanks

Jon

On 29 June 2018 at 07:19, Jon Wolfers <[hidden email]> wrote:
Hi all,

I'm trying to add a member to a mailchimp list using RexxCurl.

The mailchimp api docs give this sample curl call

Curl \

--request POST \

--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \

--user 'anystring:apikey' \

--header 'content-type: application/json' \

--data '{"email_address":"[hidden email]", "status":"subscribed"}' \

--include

My problem seems to be including my data.
I have a PHP sample from stackoverflow which includes the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

and curlopt_postfields does seem to be the way to go, however, the rexxcurl wrapper for CURLOPT_POSTFIELDS is HTTPPOSTFIELDS and the docs say

The passed parameter is a Rexx array, with each item in the array a name/value pair. eg field.1 = 'email=[hidden email]' and field.0 is the number items in the array.

I can set the field to data like this

   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'

   data.0 = 1
   data.1 = 'data='||json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

or I can ignore Mark's instruction like this

   data.0 = 1
   data.1 = json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

but in either case I get a return from the server which I believe means that no data is found:

,"title":"Invalid Resource"
,"status":400
,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"5d65323d-ce8e-4f23-9bbe-3f91279ef563"
,"errors":[{"field":"","message":"Schema describes object, NULL found instead"}]
}

Any suggestions?  I tried using HTTPPOSTDATA but the server did not recognise the request.

thanks Jon


_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

Re: [RexxLA] HTTP Post with rexxcurl

Mark Hessling

Hi Jon,

With the attempt to use HTTPPOSTDATA did you add a "content-type:" header to the CURL request specifying "application/json" via HTTPHEADER, and set the parameter for HTTPPOSTDATA as the JSON string?

Are you able to send me a simple test case that I can run here?

Cheers, Mark


On 29/06/18 16:49, Jon Wolfers wrote:
I should add, when I try to use HTTPPOSTDATA the response is 

HTTP/1.1 411 Length Required
Your browser sent a request that this server could not understand.<P>
Reference&#32;&#35;7&#46;7367a5c&#46;1530254748&#46;2b0e18

thanks

Jon

On 29 June 2018 at 07:19, Jon Wolfers <[hidden email]> wrote:
Hi all,

I'm trying to add a member to a mailchimp list using RexxCurl.

The mailchimp api docs give this sample curl call

Curl \

--request POST \

--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \

--user 'anystring:apikey' \

--header 'content-type: application/json' \

--data '{"email_address":"[hidden email]", "status":"subscribed"}' \

--include

My problem seems to be including my data.
I have a PHP sample from stackoverflow which includes the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

and curlopt_postfields does seem to be the way to go, however, the rexxcurl wrapper for CURLOPT_POSTFIELDS is HTTPPOSTFIELDS and the docs say

The passed parameter is a Rexx array, with each item in the array a name/value pair. eg field.1 = 'email=[hidden email]' and field.0 is the number items in the array.

I can set the field to data like this

   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'

   data.0 = 1
   data.1 = 'data='||json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

or I can ignore Mark's instruction like this

   data.0 = 1
   data.1 = json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

but in either case I get a return from the server which I believe means that no data is found:

,"title":"Invalid Resource"
,"status":400
,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"5d65323d-ce8e-4f23-9bbe-3f91279ef563"
,"errors":[{"field":"","message":"Schema describes object, NULL found instead"}]
}

Any suggestions?  I tried using HTTPPOSTDATA but the server did not recognise the request.

thanks Jon



_______________________________________________
rexxla-members mailing list -- [hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members

-- 
------------------------------------------------------------------------
* Mark Hessling, [hidden email] http://www.rexx.org/
* Author of THE, a Free XEDIT/KEDIT editor and, Rexx/SQL, Rexx/CURL, etc.
* Maintainer of Regina Rexx interpreter
* Use Rexx? join the Rexx Language Association: http://www.rexxla.org/ 
------------------------------------------------------------------------

_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

Re: [RexxLA] HTTP Post with rexxcurl

sahananda
Hi Mark,

Good to hear from you.  

I've pulled out the code related to rexx/curl and hard coded the values below - watch out for a line wrap in the JSON string.
Even if you succeed in posting, it won't take without my application key, which I can't post to the list, but I can get to you some way or another - do you whatsapp perhaps?

many thanks,

Jon

=========================================================


   key  = '?????' /* <<< I will send you this separately to paste in */
   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'


   /* load the RexxcURL library */
   signal on syntax name curlLoadError
      Call RxFuncAdd 'CurlLoadFuncs', 'rexxcurl', 'CurlLoadFuncs'
      Call CurlLoadFuncs
   signal off syntax

   cUrl = CurlInit()
   if cUrl = ''
   then call checkcurl 'load'
   else do
      call Curlsetopt cUrl, 'USERPWD', 'studio:'||key
      call checkCurl 'userpwd'

      call Curlsetopt cUrl, 'HTTPPOST', .true
      call checkCurl 'httppost'

      call Curlsetopt cUrl, 'HEADER', .true   -- include
      call checkCurl 'header'

      call curlSetopt cUrl, 'SSLVERIFYHOST', .false
      call checkCurl 'sslverifyhost'

      call curlSetopt cUrl, 'SSLVERIFYPEER', .false
      call checkCurl 'sslverifypeer'

      call curlSetopt cUrl, 'TIMEOUT', 50
      call checkCurl 'timeout'

      call curlSetopt curl, 'URL', url
      call checkCurl 'url'

      headers.0 = 2
      headers.1 = 'content-type: application/json'
      headers.2 = 'content-length:' length(json)
      call curlSetOpt curl, 'HEADERS', 'headers.'
      call checkcurl 'HEADERS'

      data.0 = 1
/*    data.1 = 'data='json                            */
      data.1 = json
/*    call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.' */
      call curlSetOpt curl, 'HTTPPOSTDATA', 'data.'
      call checkcurl 'HTTPPOSTDATA/FIELDS'

      call CurlPerform curl
      call checkcurl 'perform'
   end /* DO */

   call curlCleanup cUrl
   call curlDropFuncs

RETURN


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
checkCurl: procedure expose curlerror. curl
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
use arg tag

   select
      when curlerror.intcode = 0 then nop
      when curlerror.intcode = 1 then Say tag 'Internal error:' curlerror.intcode  curlerror.interrm
      otherwise                       Say tag 'CURL error:'     curlerror.curlcode curlerror.curlerrm
   end /* select */
   if curlerror.intcode \= 0
   then do
      call curlCleanup cUrl
      call curlDropFuncs

      EXIT
   end /* DO */

RETURN


On 29 June 2018 at 11:11, Mark Hessling <[hidden email]> wrote:

Hi Jon,

With the attempt to use HTTPPOSTDATA did you add a "content-type:" header to the CURL request specifying "application/json" via HTTPHEADER, and set the parameter for HTTPPOSTDATA as the JSON string?

Are you able to send me a simple test case that I can run here?

Cheers, Mark


On 29/06/18 16:49, Jon Wolfers wrote:
I should add, when I try to use HTTPPOSTDATA the response is 

HTTP/1.1 411 Length Required
Your browser sent a request that this server could not understand.<P>
Reference&#32;&#35;7&#46;7367a5c&#46;1530254748&#46;2b0e18

thanks

Jon

On 29 June 2018 at 07:19, Jon Wolfers <[hidden email]> wrote:
Hi all,

I'm trying to add a member to a mailchimp list using RexxCurl.

The mailchimp api docs give this sample curl call

Curl \

--request POST \

--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \

--user 'anystring:apikey' \

--header 'content-type: application/json' \

--data '{"email_address":"[hidden email]", "status":"subscribed"}' \

--include

My problem seems to be including my data.
I have a PHP sample from stackoverflow which includes the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

and curlopt_postfields does seem to be the way to go, however, the rexxcurl wrapper for CURLOPT_POSTFIELDS is HTTPPOSTFIELDS and the docs say

The passed parameter is a Rexx array, with each item in the array a name/value pair. eg field.1 = 'email=[hidden email]' and field.0 is the number items in the array.

I can set the field to data like this

   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'

   data.0 = 1
   data.1 = 'data='||json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

or I can ignore Mark's instruction like this

   data.0 = 1
   data.1 = json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

but in either case I get a return from the server which I believe means that no data is found:

,"title":"Invalid Resource"
,"status":400
,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"5d65323d-ce8e-4f23-9bbe-3f91279ef563"
,"errors":[{"field":"","message":"Schema describes object, NULL found instead"}]
}

Any suggestions?  I tried using HTTPPOSTDATA but the server did not recognise the request.

thanks Jon



_______________________________________________
rexxla-members mailing list -- [hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members

-- 
------------------------------------------------------------------------
* Mark Hessling, [hidden email] http://www.rexx.org/
* Author of THE, a Free XEDIT/KEDIT editor and, Rexx/SQL, Rexx/CURL, etc.
* Maintainer of Regina Rexx interpreter
* Use Rexx? join the Rexx Language Association: http://www.rexxla.org/ 
------------------------------------------------------------------------

_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members



_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

[RexxLA] Fwd: HTTP Post with rexxcurl

sahananda
Hi Mark,

I realise I made a cut and paste error with the code I sent you.
Instead of

      call curlSetOpt curl, 'HEADERS', 'headers.'

it should have read

      call curlSetOpt curl, 'HTTPHEADER', 'headers.'

and using postfields and verbose I see that the headers are indeed

Content-Length: 143
Content-Type: application/json

and 144 out of 144 bytes sent off. 

In search of the extra byte I turned debug on and found that an '&' was prepended to the data and I wonder if this isn't why the request is not recognised.  A bit confusing of the mailchimp server to call it a null in that case.  I went to the mailchimp playground and submitted that JSON with an & prepended and indeed got the null error.

If the HTTPPOSTFIELDS behaviour is correct in prepending the & then I should be using HTTPPOSTDATA (which debug shows correctly accepts the data), but that still returns with a  curlerror of 5, curlerrm of INCOMPLETE.

Looking here at the libcurl error codes error 5 would be  CURLE_COULDNT_RESOLVE_PROXY and none of the error texts contain the string INCOMPLETE.
I don't understand how proxies come into this, however, using HTTPPOSTDATA and call Curlsetopt cUrl, 'NOPROXY', '*' we still get an error 5.

Here is part of the debug trace from the curlSetOpt in case it helps (posting the whiole lot makes this message too long for rexxla rules):
                                  
++ Call CURLSETOPT from thread 6952
++   1: "40751240" Length: 8
++   2: "HTTPPOSTDATA" Length: 12
++   3: "data." Length: 5
>>>> Call GetRexxVariableNumber(data.,67d5e0,0) from thread 6952
>>>> Call GetRexxVariable(data.,67d600,1) from thread 6952
>>>> Call CurlSetopt(RXCURLOPT_POST_DATA)(Variable: <data.1> Value: <{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}>) from thread 6952
>>>> Call SetIntError(d:\projects\rexxcurl\rexxcurl.c,2779,1,Error from cURL) from thread 6952
>>>> Call RxGetRunFlags() from thread 6952
>>>> Call SetRexxVariable("CURLERROR.INTERRM",17,"Rexx/CURL-01: Error from cURL [d:\projects\rexxcurl\rexxcurl.c:2779]",68) from thread 6952
 >>>> Call SetRexxVariable("CURLERROR.INTCODE",17,"-1",2) from thread 6952
>>>> Call SetCURLError(5,INCOMPLETE) from thread 6952
>>>> Call SetRexxVariable("CURLERROR.CURLERRM",18,"INCOMPLETE",10) from thread 6952
>>>> Call SetRexxVariable("CURLERROR.CURLCODE",18,"5",1) from thread 6952
>>>> Call RxReturnStringAndFree(67d658,"" Length: 0 Free: 0) from thread 6952
>>>> Call RxReturnDataAndFree(67d658,"" Length: 0 Free: 0) from thread 6952
++ Exit CURLSETOPT with length: 0: Data:


I hope you can point me a way forward with this.

Thanks  Jon



_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

Re: [RexxLA] HTTP Post with rexxcurl

Mark Hessling
In reply to this post by sahananda

Hi Jon,

There is an error with HTTPPOSTDATA. Will look into it.

However, it seems that HTTPPOSTDATA is not what you need for your issue. From testing with my server, the mailchimp cURL example sends the JSON string as raw data, not as key/value pairs.

What seems to work for my server, using cURL command line and Rexx/CURL is to use INSTEM option; this sends raw data.

Try the amended (in red) code below.

Cheers, Mark

On 29/06/18 21:31, Jon Wolfers wrote:
Hi Mark,

Good to hear from you.  

I've pulled out the code related to rexx/curl and hard coded the values below - watch out for a line wrap in the JSON string.
Even if you succeed in posting, it won't take without my application key, which I can't post to the list, but I can get to you some way or another - do you whatsapp perhaps?

many thanks,

Jon

=========================================================


   key  = '?????' /* <<< I will send you this separately to paste in */
   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'


   /* load the RexxcURL library */
   signal on syntax name curlLoadError
      Call RxFuncAdd 'CurlLoadFuncs', 'rexxcurl', 'CurlLoadFuncs'
      Call CurlLoadFuncs
   signal off syntax

   cUrl = CurlInit()
   if cUrl = ''
   then call checkcurl 'load'
   else do
      call Curlsetopt cUrl, 'USERPWD', 'studio:'||key
      call checkCurl 'userpwd'

      call Curlsetopt cUrl, 'HTTPPOST', .true
      call checkCurl 'httppost'

      call Curlsetopt cUrl, 'HEADER', .true   -- include
      call checkCurl 'header'

      call curlSetopt cUrl, 'SSLVERIFYHOST', .false
      call checkCurl 'sslverifyhost'

      call curlSetopt cUrl, 'SSLVERIFYPEER', .false
      call checkCurl 'sslverifypeer'

      call curlSetopt cUrl, 'TIMEOUT', 50
      call checkCurl 'timeout'

      call curlSetopt curl, 'URL', url
      call checkCurl 'url'

      headers.0 = 3
      headers.1 = 'content-type: application/json'
      headers.2 = 'content-length:' length(json)
        headers.3 = 'Expect:' -- stops return status of 100
      call curlSetOpt curl, 'HTTPHEADER', 'headers.'
      call checkcurl 'HEADERS'

      data.0 = 1
/*    data.1 = 'data='json                            */
      data.1 = json
/*    call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.' */
      call curlSetOpt curl, 'INSTEM', 'data.'
      call checkcurl 'HTTPPOSTDATA/FIELDS'

      call CurlPerform curl
      call checkcurl 'perform'
   end /* DO */

   call curlCleanup cUrl
   call curlDropFuncs

RETURN


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
checkCurl: procedure expose curlerror. curl
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
use arg tag

   select
      when curlerror.intcode = 0 then nop
      when curlerror.intcode = 1 then Say tag 'Internal error:' curlerror.intcode  curlerror.interrm
      otherwise                       Say tag 'CURL error:'     curlerror.curlcode curlerror.curlerrm
   end /* select */
   if curlerror.intcode \= 0
   then do
      call curlCleanup cUrl
      call curlDropFuncs

      EXIT
   end /* DO */

RETURN


On 29 June 2018 at 11:11, Mark Hessling <[hidden email]> wrote:

Hi Jon,

With the attempt to use HTTPPOSTDATA did you add a "content-type:" header to the CURL request specifying "application/json" via HTTPHEADER, and set the parameter for HTTPPOSTDATA as the JSON string?

Are you able to send me a simple test case that I can run here?

Cheers, Mark


On 29/06/18 16:49, Jon Wolfers wrote:
I should add, when I try to use HTTPPOSTDATA the response is 

HTTP/1.1 411 Length Required
Your browser sent a request that this server could not understand.<P>
Reference&#32;&#35;7&#46;7367a5c&#46;1530254748&#46;2b0e18

thanks

Jon

On 29 June 2018 at 07:19, Jon Wolfers <[hidden email]> wrote:
Hi all,

I'm trying to add a member to a mailchimp list using RexxCurl.

The mailchimp api docs give this sample curl call

Curl \

--request POST \

--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \

--user 'anystring:apikey' \

--header 'content-type: application/json' \

--data '{"email_address":"[hidden email]", "status":"subscribed"}' \

--include

My problem seems to be including my data.
I have a PHP sample from stackoverflow which includes the line

curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);

and curlopt_postfields does seem to be the way to go, however, the rexxcurl wrapper for CURLOPT_POSTFIELDS is HTTPPOSTFIELDS and the docs say

The passed parameter is a Rexx array, with each item in the array a name/value pair. eg field.1 = 'email=[hidden email]' and field.0 is the number items in the array.

I can set the field to data like this

   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'

   data.0 = 1
   data.1 = 'data='||json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

or I can ignore Mark's instruction like this

   data.0 = 1
   data.1 = json
   call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.'

but in either case I get a return from the server which I believe means that no data is found:

,"title":"Invalid Resource"
,"status":400
,"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.","instance":"5d65323d-ce8e-4f23-9bbe-3f91279ef563"
,"errors":[{"field":"","message":"Schema describes object, NULL found instead"}]
}

Any suggestions?  I tried using HTTPPOSTDATA but the server did not recognise the request.

thanks Jon



_______________________________________________
rexxla-members mailing list -- [hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members

-- 
------------------------------------------------------------------------
* Mark Hessling, [hidden email] http://www.rexx.org/
* Author of THE, a Free XEDIT/KEDIT editor and, Rexx/SQL, Rexx/CURL, etc.
* Maintainer of Regina Rexx interpreter
* Use Rexx? join the Rexx Language Association: http://www.rexxla.org/ 
------------------------------------------------------------------------

_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members




_______________________________________________
rexxla-members mailing list -- [hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members

-- 
------------------------------------------------------------------------
* Mark Hessling, [hidden email] http://www.rexx.org/
* Author of THE, a Free XEDIT/KEDIT editor and, Rexx/SQL, Rexx/CURL, etc.
* Maintainer of Regina Rexx interpreter
* Use Rexx? join the Rexx Language Association: http://www.rexxla.org/ 
------------------------------------------------------------------------

_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members
Reply | Threaded
Open this post in threaded view
|

Re: [RexxLA] HTTP Post with rexxcurl

sahananda
In reply to this post by sahananda
Hi Mark,

Thank you so much for your help.

I've now got my code working, though I couldn't use INSTEM, I had to use INFILE instead.  I think I have always had problems with INSTEM on Windows, and I include some diagnostics below in case they are helpful.  Now that I see that INSTEM writes the file to disk anyway, i'm quite happy to use INFILE.

---------------------------------
Unfortunately, and weirdly INSTEM returns an intcode of -6.

++ Call CURLSETOPT from thread 236
++   1: "39833736" Length: 8
++   2: "INSTEM" Length: 6
++   3: "data." Length: 5
>>>> Call GetRexxVariableNumber(data.,3ed67c,0) from thread 236
>>>> Call SetIntError(d:\projects\rexxcurl\rexxcurl.c,2467,6,Invalid filename) from thread 236
>>>> Call RxGetRunFlags() from thread 236
>>>> Call SetRexxVariable("CURLERROR.INTERRM",17,"Rexx/CURL-06: Invalid filename [d:\projects\rexxcurl\rexxcurl.c:2467]",69) from thread 236
*DEBUG* Setting variable "CURLERROR.INTERRM" to "Rexx/CURL-06: Invalid filename [d:\projects\rexxcurl\rexxcurl.c:2467]".
*DEBUG* NOError Setting variable "CURLERROR.INTERRM" to "Rexx/CURL-06: Invalid filename [d:\projects\rexxcurl\rexxcurl.c:2467]". OK.
>>>> Call SetRexxVariable("CURLERROR.INTCODE",17,"-6",2) from thread 236
*DEBUG* Setting variable "CURLERROR.INTCODE" to "-6".
*DEBUG* NOError Setting variable "CURLERROR.INTCODE" to "-6". OK.

This is rexxcurl 2.0 on Windows 10 home.

I'm not a C programmer, so completely out of my depth.  I see that you are getting a tempfile using the c tmpfile() function. I don't see an include for stdio.h - could that be it?.

I don't know if the tmpfile function relies on the TMP or TEMP environment settings, but they both point to the same folder on my machine which has write access.
TEMP=C:\Users\Saha\AppData\Local\Temp
TMP=C:\Users\Saha\AppData\Local\Temp
and I manage to write my tempfile there

If this is a bug rather than just a weirdness on my laptop I'm happy to assist with debugging/testing.

Jon


On 30 June 2018 at 00:02, Mark Hessling <[hidden email]> wrote:

Hi Jon,

There is an error with HTTPPOSTDATA. Will look into it.

However, it seems that HTTPPOSTDATA is not what you need for your issue. From testing with my server, the mailchimp cURL example sends the JSON string as raw data, not as key/value pairs.

What seems to work for my server, using cURL command line and Rexx/CURL is to use INSTEM option; this sends raw data.

Try the amended (in red) code below.

Cheers, Mark


On 29/06/18 21:31, Jon Wolfers wrote:
Hi Mark,

Good to hear from you.  

I've pulled out the code related to rexx/curl and hard coded the values below - watch out for a line wrap in the JSON string.
Even if you succeed in posting, it won't take without my application key, which I can't post to the list, but I can get to you some way or another - do you whatsapp perhaps?

many thanks,

Jon

=========================================================


   key  = '?????' /* <<< I will send you this separately to paste in */
   json = '{"email_address":"[hidden email]","status":"subscribed","merge_fields":{"FNAME":"Fred","LNAME":"Testperson","ADDRESS":"","PHONE":"","STUDIOID":0}}'


   /* load the RexxcURL library */
   signal on syntax name curlLoadError
      Call RxFuncAdd 'CurlLoadFuncs', 'rexxcurl', 'CurlLoadFuncs'
      Call CurlLoadFuncs
   signal off syntax

   cUrl = CurlInit()
   if cUrl = ''
   then call checkcurl 'load'
   else do
      call Curlsetopt cUrl, 'USERPWD', 'studio:'||key
      call checkCurl 'userpwd'

      call Curlsetopt cUrl, 'HTTPPOST', .true
      call checkCurl 'httppost'

      call Curlsetopt cUrl, 'HEADER', .true   -- include
      call checkCurl 'header'

      call curlSetopt cUrl, 'SSLVERIFYHOST', .false
      call checkCurl 'sslverifyhost'

      call curlSetopt cUrl, 'SSLVERIFYPEER', .false
      call checkCurl 'sslverifypeer'

      call curlSetopt cUrl, 'TIMEOUT', 50
      call checkCurl 'timeout'

      call curlSetopt curl, 'URL', url
      call checkCurl 'url'

      headers.0 = 3
      headers.1 = 'content-type: application/json'
      headers.2 = 'content-length:' length(json)
        headers.3 = 'Expect:' -- stops return status of 100
      call curlSetOpt curl, 'HTTPHEADER', 'headers.'
      call checkcurl 'HEADERS'

      data.0 = 1
/*    data.1 = 'data='json                            */
      data.1 = json
/*    call curlSetOpt curl, 'HTTPPOSTFIELDS', 'data.' */
      call curlSetOpt curl, 'INSTEM', 'data.'
      call checkcurl 'HTTPPOSTDATA/FIELDS'

      call CurlPerform curl
      call checkcurl 'perform'
   end /* DO */

   call curlCleanup cUrl
   call curlDropFuncs

RETURN


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
checkCurl: procedure expose curlerror. curl
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
use arg tag

   select
      when curlerror.intcode = 0 then nop
      when curlerror.intcode = 1 then Say tag 'Internal error:' curlerror.intcode  curlerror.interrm
      otherwise                       Say tag 'CURL error:'     curlerror.curlcode curlerror.curlerrm
   end /* select */
   if curlerror.intcode \= 0
   then do
      call curlCleanup cUrl
      call curlDropFuncs

      EXIT
   end /* DO */

RETURN



_______________________________________________
rexxla-members mailing list -- mailto:[hidden email]
http://rexxla.org/mailman/listinfo/rexxla-members