11 Replies Latest reply on Sep 23, 2016 7:13 PM by mikethecreative

    HTTP Post Content Type

    mikethecreative

      I'm trying to POST to an API that requires the Content Type to be "application/xml." The only solution I'm finding online is to use the Base Elements plug in.

       

      The Custom Headers I'm needing to send over are:

      user: XXXXXX

      pwd: XXXXXX

      lan: en

      Content Type: application/xml

       

       

      ------

       

       

      With the base elements plugin, I did the following:

       

      Set Variable $$xmlHeaders ;

      BE_HTTP_Set_Custom_Header ( "Content-Type" ; "application/xml" ) &

       

       

      BE_HTTP_Set_Custom_Header ( "user" ; "XXXXXX" ) &

       

       

      BE_HTTP_Set_Custom_Header ( "pwd" ; "XXXXXX" ) &

       

       

      BE_HTTP_Set_Custom_Header ( "lan" ; "en" )

      Set Variable $$xmlPost ;

      BE_HTTP_POST ( "http://api.website.com/order-api/orderService?" ; "xml=" & $$xmlMaster )

       

       

      ------

       

       

      In the Data Viewer, I'm getting the following:

       

      $$xmlHeaders ;

      HTTP/1.1 100 Continue

       

       

      HTTP/1.1 400 Bad Request

      Server: nginx/1.4.4

      Date: Fri, 23 Sep 2016 07:25:48 GMT

      Content-Type: text/html

      Content-Length: 158

      Connection: keep-alive

       

      $$xmlPost ;

      javax.xml.bind.UnmarshalException

      - with linked exception:

      [org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.]

       

       

       

      For BE_HTTP_Response_Code I'm getting:

      400

       

      For BE_HTTP_Response_Headers I'm getting:

      HTTP/1.1 100 Continue

       

       

      HTTP/1.1 400 Bad Request

      Server: nginx/1.4.4

      Date: Fri, 23 Sep 2016 07:25:48 GMT

      Content-Type: text/html

      Content-Length: 158

      Connection: keep-alive

       

       

      Any help would be appreciated. Maybe I don't need this plugin? I'm really not too sure. I know it's not the XML that Filemaker is generating because I"m able to use a Firefox Extension to POST and it works fine. But I really want to be able to Post via Filemaker and not have to use an additional app.

        • 1. Re: HTTP Post Content Type
          nicolai

          Base_Elements (or other plugin) is the way to go if you need to set headers - you can't do it in FileMaker natively.

           

          Looking at your responses, it looks like there is something wrong with the XML you are posting. Try to check if it is well-formed. If you have xsd, try to validate it.

           

          Nicolai

          • 2. Re: HTTP Post Content Type
            wimdecorte

            Agreed with nicolai, the problem seems to be in the payload you are sending.   The BE plugin has a trace function that will give you a lot more output, might be useful.

             

            Another thing that could be useful is to use a tool like POSTMAN or SOAPUI to test the request.

            • 3. Re: HTTP Post Content Type
              mikethecreative

              If I copy the $$xmlMaster content from the data viewer, I'm able to use a Firefox Posting Extension (called Poster) to post the data. so the XML data that is being generated by FileMaker works fine. It has to be the way this plugin is trying to post it or somewhere where I don't have something set correctly.

               

              Notice the error shows "Content-Type: text/html." That should be "application/xml."

              • 4. Re: HTTP Post Content Type
                nicolai

                +1  wimdecorte for POSTMAN and SOUPUI, although latest version of SOUPUI is very slow on my mac.

                 

                mikethecreative

                 

                I use  BE_HTTP_Set_Custom_Header to set content-type all the time and did not have a problem so far.

                 

                Make sure you do not reset header somewhere in your script.

                • 5. Re: HTTP Post Content Type
                  mikethecreative

                  How do I know if I'm resetting my header somewhere in the script? Does my current $$xmlHeaders look correct? I'm stumped.

                  • 6. Re: HTTP Post Content Type
                    wimdecorte

                    I think the clue is in $$xmlPOST but also use BE_trace in your data viewer to see if it yields more clues.

                     

                    The service does not like your XML as it is formed by FM while the other tools may do subtle substitutions as you copy and paste the XML into the tools to try it there.

                     

                    You may have to specify the charset as part of setting the content type:

                     

                    Setting the HTTP charset parameter

                     

                    also: are you sure it needs to be 'application/xml' and not 'text/xml'?

                    1 of 1 people found this helpful
                    • 7. Re: HTTP Post Content Type
                      mikethecreative

                      Yes, I'm positive it has to be "application/xml." If I leave it as "text/xml" in the Firefox Extension, it won't post.

                      • 8. Re: HTTP Post Content Type
                        nicolai

                        You can reset headers by running another "BE_Set_Custom_Header ( content-type" in your script

                         

                        I am not sure if after posting the headers are being reset back to the default, so I usually set headers every time before I post or put.

                         

                        How do you set your $$xmlHeaders?

                        • 9. Re: HTTP Post Content Type
                          David Moyer

                          Hi,

                          since folks are saying the xml isn't well formed, it occurred to me that there might be funky characters within the data.  I use this to format single elements (but in a custom function) ...

                          "<" & $tag & ">" &

                          Substitute ( $data;

                            ["<"; "&lt"];

                            [">"; "&gt"];

                            ["&"; "&amp"];

                            ["\""; "&quot"];

                            ["'"; "&apos"]

                          ) &

                          "</" & $tag & ">"

                           

                          I'm on Windows.  When I want to verify xml, I paste it into Notepad and save it as test.xml.  Then I open test.xml in Internet Explorer.

                          • 10. Re: HTTP Post Content Type
                            mikethecreative

                            Hey all, thank you so much for the help.  I am adding some screenshots to better explain what's happening.

                             

                            Screenshots of an extension called "Poster" that I use in Firefox. I set a few parameters and copy my XML content from the data viewer in Filemaker and I'm able to POST to our API.

                             

                            Screen 01 (content to send) contains: URL, Content Type, XML Content

                            Screen 02 (headers) contains: URL, User, Password and Language

                             

                            Screen 01:

                            screen01-xml.png

                             

                            Screen 02:

                            screen02-headers.png

                             

                             

                            I'm wanting to do this in Filemaker but I can't figure out how. This is the script I wrote using the Base Elements plugin. The errors I'm getting are at the top of this thread.

                             

                            screen03-script.png

                             

                            Maybe I'm not setting the headers or something correctly. I'm not too sure.

                            • 11. Re: HTTP Post Content Type
                              mikethecreative

                              YAAAASSS I JUST FIGURED IT OUT. I don't need the "xml=" before the $$xmlMaster on the BE_HTTP_POST. I noticed that it was getting sent over as plain text before the XML when I used the BE_Curl_Trace function.

                               

                              Ughhh, I'm such a newb.

                               

                              Thanks so much for the help everyone!!

                               

                              P.S. If you see something that needs improvement in my script, please share.