ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Using x-www-form-urlencoded in RPGLE

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using x-www-form-urlencoded in RPGLE

    I need to do a post to a server to retrieve a token. Currently I have it set up to use content type:x-www-form-urlencoded in the header but am unfamiliar with how to create the form body. I tried creating a JSON payload but it fails with bad request. Any information on how I can get this to work, would be greatly appreciated.

    Here is my code for the header and json body:

    Header:
    @Headers =
    'Cache-Control: no-cache' + @CrLf +
    'content-type: x-www-form-urlencoded' + @Crlf +
    'accept: */*' + @Crlf;

    Body:
    yajl_beginObj();
    yajl_addchar('grant_type':'client_credentials');
    yajl_addchar('client_id': %Trim(MMid));
    yajl_addchar('client_secret': %Trim(MMSecret));
    yajl_endObj();

    %len(@jsonData) = @JSON_BUF_SIZE;
    @rc = yajl_copyBuf(0
    : %addr(@jsonData: *data)
    : @JSON_BUF_SIZE
    : @len );
    %len(@jsonData) = @len;

    yajl_savebuf(%Trim(@AuthFile): @Error);

    http_debug(*On:'/mastery/authdebug.txt');
    http_setccsids(1208:0);
    Http_xproc(HTTP_POINT_ADDL_HEADER
    : %paddr($Add_Headers));

    @rc = Http_url_post(%Trim(@Uri)
    : %Addr(@jsondata) + 2
    : %Len(%Trim(@jsondata))
    : @AuthFile
    : 15
    : Http_UserAgent
    : 'x-www-form-urlencoded ');

  • #2
    JSON and x-www-form-urlencoded are two completely different data formats. Why are you creating JSON if you want a URL-encoded form?

    HTTPAPI comes with several examples of coding www forms. For example, EXAMPLE15 has code like this to build a form:

    Code:
    Form = WEBFORM_open();
    
    WEBFORM_SetVar(Form: 'from': fromAddr );
    WEBFORM_SetVar(Form: 'subject': subject);
    WEBFORM_SetVar(Form: 'Comment': message);
    
    WEBFORM_postData( Form : myPointer: dataSize );
    Take a look at the examples for more infomation.

    Comment


    • #3
      Thank you for your response. I actually figured it out. I excluded the headers and created a @Form_Data field to put the form data in. Then I used the HTTP_STRING to Post to the server. Got back their JSON response. Here is the code I used.

      @form_data = 'grant_type=client_credentials'
      + '&' + 'client_id=' + %Trim(mmid)
      + '&' + 'client_secret=' + %Trim(mmsecret);

      Monitor;
      MMResp = http_string('POST': @uri : %trim(@form_data) :
      'application/x-www-form-urlencoded');

      Comment


      • #4

        Originally posted by ccacmm2016 View Post
        Thank you for your response. I actually figured it out. I excluded the headers and created a @Form_Data field to put the form data in. Then I used the HTTP_STRING to Post to the server. Got back their JSON response. Here is the code I used.

        @form_data = 'grant_type=client_credentials'
        + '&' + 'client_id=' + %Trim(mmid)
        + '&' + 'client_secret=' + %Trim(mmsecret);

        Monitor;
        MMResp = http_string('POST': @uri : %trim(@form_data) :
        'application/x-www-form-urlencoded');

        The way you're doing it doesn't actually URL encode the data. Why did you ignore my post telling you how to do it?

        Your way will work only if you never have characters that need to be encoded.

        Comment


        • #5
          Scott,

          I didn't ignore your post, I got it to work before you responded. All I needed to get was the token from the server and the way I showed you worked. I will definitely look into what you posted. Thank you.

          Comment

          Working...
          X