ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

sending JSON message from IBMi

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

  • sending JSON message from IBMi

    I have installed Scott Klement's HTTPAPI and am able to post
    a JSON formated test file to a remote server...

    but I cannot get the content-type correct.

    so i took example 17 and tried to force this:
    Code:
    ContentType = 'application/json';
    I know this can't work as is....but unsure of where to go from here.

    I am aware of YAJL from Scott's site.

    Just seems like I'm so close.


    Can someone point me in the right direction?

    Thank you
    Jamie
    All my answers were extracted from the "Big Dummy's Guide to the As400"
    and I take no responsibility for any of them.

    www.code400.com

  • #2
    Re: sending JSON message from IBMi

    What is not working? Is the server responding with an error message about the ContentType? Might have to ask what ContentType it is expecting.

    Comment


    • #3
      Re: sending JSON message from IBMi

      error message is:



      Because I am using EXAMPLE 17 and it is specifically for

      PHP Code:
            This example demonstrates posting multipart/form-data to a
            
      web server.
            *
            * 
      It is intended to mimic the following HTML form:
            *----------------------------------------------------------------
            *
            * <
      form method="post" enctype="multipart/form-data"
            
      *       action="http://www.scottklement.com/cgi-bin/upload.cgi">
            *
            *   <
      input type="hidden" name="operation" value="VERIFY">
            *
            *   
      File Format:
            *   <
      input type="radio"  name="data_format" value="HTML" checked>HTML
            
      *   <input type="radio"  name="data_format" value="PDF">PDF
            
      *   <input type="radio"  name="data_format" value="PTF">RTF<br>
            *
            *   
      File to send:
            *   <
      input type="file" name="tutorial"><br>
            *
            *   <
      input type="submit">
            *
            * </
      form>
            *
            *----------------------------------------------------------------
            *
           
      H DFTACTGRP(*NOBNDDIR('HTTPAPI')

            /
      copy qrpglesrc,httpapi_h
            
      /copy qrpglesrc,ifsio_h

           D PSDS           SDS                  Qualified
           D   jobno               264    269A

           D tempFile        s            200A   varying
           D ContentType     s             64A
           D enc             s               
      *
           
      D msg             s             52A
           D rc              s             10I 0

            
      /free

                
      *inlr = *on;

                
      //  Create a temporary filename that won't conflict with
                //  another job.

                
      tempFile '/tmp/encoded-data-' psds.jobno;

                
      //  HTTPAPI's multipart/form-data encoding functions output
                //    two things:
                //
                //       1) A stream file field suitable for use with
                //            http_url_post_stmf()
                //       2) A content-type field suitable for use with
                //            http_url_post_stmf()
                //
                //  The http_mfd_encoder_open() API opens the stream file
                //  and initializes the encoding routines.  You must call
                //  that first:

                
      ContentType 'application/json';
                
      enc http_mfd_encoder_opentempFile ContentType );
                if (
      enc = *NULL);
                   
      msg http_error();
                   
      dsply msg;
                   return;
                endif;

                
      //
                //  now you can add variables to the encoded stream file:
                //

                 //http_mfd_encoder_addvar_s(enc: 'operation'  : 'VERIFY');
                 //http_mfd_encoder_addvar_s(enc: 'data_format': 'PDF'   );

                //
                //  and you can even add the contents of another stream
                //  file, compatible with the <input type="file"> HTML
                //  keyword.
                //
                //  In this case, the Socket Tutorial that was downloaded
                //  in EXAMPLE1 will be added to the temp file
                //

                
      http_mfd_encoder_addstmfenc
                                        
      'file'
                                        
      '/home/xjf90405/junk.txt'
                                        
      'application/JSON');

                
      //
                // once all of the variables/files have been added, the
                // http_mfd_encoder_close() API must be called to clean
                // up. (The stream file will remain on disk so that you
                // can use it with http_url_post_stmf)
                //

                
      http_mfd_encoder_closeenc );


                
      //
                //  post the results to the web server
                //

                
      rc http_url_post_stmf('http://somewhere.com' +
                                        
      '/abc/api/v1/inventory '
                                       
      tempFile
                                       
      '/home/xjf90405/http_result.txt'
                                       
      HTTP_TIMEOUT
                                       
      HTTP_USERAGENT
                                       
      ContentType );

                if (
      rc <> 1);
                   
      msg http_error();
                   
      dsply msg;
                   return;
                endif;

                
      //
                //  delete temp files, we're done
                //

               //  unlink('/tmp/http_result.txt');
               //   unlink(tempFile);

                
      return;
            /
      end-free 

      and its using copy book HTTPAPI_H
      specifically
      PHP Code:
      D http_url_post_stmf...                                                                
      D                 PR            10I 0                                                  
      D  peURL                     32767A   varying 
      const options(*varsize)                  
      D  pePostFile                32767A   varying const options(*varsize)                  
      D  peRecvFile                32767A   varying const options(*varsize)                  
      D  peTimeout                    10I 0 value options(*nopass)                           
      D  peUserAgent                  64A   const options(*nopass:*omit)                     
      D  peContentType                64A   const options(*nopass:*omit)                     
      D  peSOAPAction                 64A   const options(*nopass:*omit
      I believe there is another http post api I should use
      Attached Files
      Last edited by jamief; September 3, 2014, 01:47 PM.
      All my answers were extracted from the "Big Dummy's Guide to the As400"
      and I take no responsibility for any of them.

      www.code400.com

      Comment


      • #4
        Re: sending JSON message from IBMi

        I'm very surprised to see you using the multipart form data encoder. But, if that's what the web service calls for, then it'll work. HOWEVER, the contentType field in your example should be set to 'multipart/form-data' NOT application/json.

        The idea behind the MFD encoder is that it's for uploading many different "parts" of data, so you can have more than one file uploaded, for example. Or a combinarion between one file and multiple form fields, etc. This is commonly done on HTML forms that allow for a file upload along with form fields.

        So, the "overall" content-type in that case MUST be "multipart/form-data". That's how the receiving program knows you're sending a multipart document. Then there's also a separate content-type sent for each part that's in the multipart document. So the overall must be "multipart/form-data" (that's the one you use on http_post_stmf) but the individual pieces might be application/json or something like that. You specify the individual pieces when you call routines like http_mfd_encoder_addstmf().

        So please try changing the 'cotnentType' variable to multipart/form-data.

        Also, the reason this surprised me is that web services don't usualyl use multipart/form-data. (That's not to say that none of them do -- some do, it's just very uncommon.) Multipart documents are usually used with web pages (html-based pages, for example) rather than web services. But, if that's what this one calls for, it should work.

        Comment


        • #5
          Re: sending JSON message from IBMi

          Thanks for the quick reply

          What would you recommend doing...

          This is currently being done by another team.
          Dataq's are used to send data to a server, from there JSON is used to post to external site.

          I am just trying to do the same thing from the IBMi.


          Jamie
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: sending JSON message from IBMi

            The way I approach this sort of thing is to do whatever is needed. So if you have a web service that wants a multipart/form-data, then by all means do that (don't worry about the fact that I find it unusual.)

            I don't see any reason why you couldn't do whatever is being done on the other server. But, right now you aren't doing that, and that's why you're getting an error :-)

            Comment


            • #7
              Re: sending JSON message from IBMi

              I'm a major league rookie at this stuff and reading the thread to learn. Can't follow what's going on.

              There's a statement "error message is:" and it's blank underneath. I don't see an error message and can't tell what you're indicating the problem is.

              I see Example 17 of Scott's code which says is example of posting multi-part/form-data and I think you say you are using that, but you also say that you are trying to what other team does. You give no indication that I can see what the other team is doing (for example, what their encoding type is).

              Comment


              • #8
                Re: sending JSON message from IBMi

                Originally posted by ralphdaugherty View Post
                There's a statement "error message is:" and it's blank underneath. I don't see an error message and can't tell what you're indicating the problem is.
                Jamie provided an image that shows the error message. I think he meant to put it "inline" under the text that says "the error is" but forgot, and in vBulletin when you don't tell it to put the image inline, it'll default to showing it at the end of the message. So, just look at the image he provides at the end if you want to see the message.

                Unfortunately, at this point in time, this isn't a good learning example for you, Ralph. We don't have any technical specifications of how this is supposed to work. All we have is an example of one thing that Jamie tried that didn't work.

                Jamie, if you can provide details of how this is supposed to work, I can explain how to make HTTPAPI do whatever that is. Please understand that all web services are different... so there isn't any "just do this" type help I can provide until I know exactly how this particular one is supposed to work.

                Comment


                • #9
                  Re: sending JSON message from IBMi

                  ok, thanks for clarification, Scott.

                  Comment


                  • #10
                    Re: sending JSON message from IBMi

                    Hold that thought....

                    I'm going to install the JSON service program from www.rpgnextgen.com and see if I can
                    pull out the parts I need.

                    I will get back with the results

                    Thank you
                    Jamie
                    All my answers were extracted from the "Big Dummy's Guide to the As400"
                    and I take no responsibility for any of them.

                    www.code400.com

                    Comment


                    • #11
                      Re: sending JSON message from IBMi

                      jamie,

                      are you now talking about receiving JSON in the response?

                      Comment


                      • #12
                        Re: sending JSON message from IBMi

                        I don't really need a response .. I just want to send.
                        All my answers were extracted from the "Big Dummy's Guide to the As400"
                        and I take no responsibility for any of them.

                        www.code400.com

                        Comment


                        • #13
                          Re: sending JSON message from IBMi

                          Jamie

                          fine, there are several ways to go, procedural or OO oriented ;-)

                          Comment


                          • #14
                            Originally posted by Scott Klement View Post
                            Re: sending JSON message from IBMi

                            I'm very surprised to see you using the multipart form data encoder. But, if that's what the web service calls for, then it'll work. HOWEVER, the contentType field in your example should be set to 'multipart/form-data' NOT application/json.

                            The idea behind the MFD encoder is that it's for uploading many different "parts" of data, so you can have more than one file uploaded, for example. Or a combinarion between one file and multiple form fields, etc. This is commonly done on HTML forms that allow for a file upload along with form fields.

                            So, the "overall" content-type in that case MUST be "multipart/form-data". That's how the receiving program knows you're sending a multipart document. Then there's also a separate content-type sent for each part that's in the multipart document. So the overall must be "multipart/form-data" (that's the one you use on http_post_stmf) but the individual pieces might be application/json or something like that. You specify the individual pieces when you call routines like http_mfd_encoder_addstmf().

                            So please try changing the 'cotnentType' variable to multipart/form-data.

                            Also, the reason this surprised me is that web services don't usualyl use multipart/form-data. (That's not to say that none of them do -- some do, it's just very uncommon.) Multipart documents are usually used with web pages (html-based pages, for example) rather than web services. But, if that's what this one calls for, it should work.
                            Just wanted to say that this post helped me a great deal. I'm working with a REST API (Mirakl) where one of the endpoints requires you to upload (POST) a CSV file using multipart/form-data (the file and another variable describing the "import_mode").

                            Following your EXAMPLE7 program was invaluable!

                            Comment


                            • #15
                              Originally posted by jamief View Post
                              Re: sending JSON message from IBMi

                              I don't really need a response .. I just want to send.

                              Jamie -

                              If you're just looking to write out JSON from an RPG program, I've found Scott's YAJL super easy to use. I don't know if you need to post a file or just JSON data. I got a little confused in the thread.

                              Code:
                              yajl_genOpen(*off);  
                              yajl_beginObj();     
                                yajl_addBool('success': true);  
                              yajl_endObj();                       
                              yajl_writeStdout(200 : errMsg);      
                              yajl_genClose();
                              And poof. You've got a web service.

                              Mike

                              Your friends list is empty!

                              Comment

                              Working...
                              X