Page 1 of 1

400 ERROR - Bad Request

Posted: Tue May 10, 2022 8:38 pm
by sgonchigar
Hello,

I am trying to upload a PDF document to a REST api - sandbox as mentioned here https://apimarketplace.uhcprovider.com/ ... attachment

it works in SoapUI and using python on iSeries. However, I am getting Bad Request when I am consuming using HTTPAPI.

Here is the debug log:
Protocol Used: TLS Version 1.3
http_persist_req(POST) entered.
http_long_ParseURL(): entered
http_long_ParseURL(): entered
do_oper(POST): entered
There are 0 cookies in the cache
POST /Claims/api/attachment/v1.0?tin=%00%C2%97123456&transactionId=%00%C2%8711111-11111-11111-111&docTypeCode=%00%02&docTypeDesc=%00%08Support+Data+for+Cla&ticketNumber=%00%0CPIQ-123456 HTTP/1.1
Host: apimarketplace.uhcprovider.com
User-Agent: http-api/1.45
Content-Type: multipart/form-data
Content-Length: 113210
Authorization:
env:
Accept-Encoding:gzip,deflate
Content-Disposition: form-data; name="files"; filename="/tmp/UHC API Marketplace pdf.pdf"

sendraw(): entered
%PDF-1.4
%Óëéá
1 0 obj
<</Creator (Mozilla/5.0 \(Windows NT 10.0; Win64; x64\) AppleWebKit/537.36 \(KHTML, like Gecko\) Chrome/101.0.4951.54 Safari/537.36)
/Producer (Skia/PDF m101)
/CreationDate (D:20220509025312+00'00')
/ModDate (D:20220509025312+00'00')>>
endobj
3 0 obj
<</ca 1
/BM /Normal>>
endobj
5 0 obj
<</Type /XObject
/Subtype /Image
/Width 2348
/Height 159
/ColorSpace /DeviceGray
/BitsPerComponent 8
/Filter /DCTDecode
/ColorTransform 0
.......................lot of pdf data..............
trailer
<</Size 63
/Root 38 0 R
/Info 1 0 R>>
startxref
111872
%%EOF
recvresp(): entered
HTTP/1.1 400 Bad Request
Server: CloudFront
Date: Tue, 10 May 2022 20:22:40 GMT
Content-Type: text/html
Content-Length: 915
Connection: close
X-Cache: Error from cloudfront
Via: 1.1 b02b0d001663966fda1c9117bcb43836.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: DFW55-C3
X-Amz-Cf-Id: zWkdS1Ah4XxWdGfz8bPp43hv-YypG3qskpMgDs74_wD4dbJ780MZBw==


SetError() #13: HTTP/1.1 400 Bad Request
recvresp(): end with 400
recvdoc parms: identity 915
header_load_cookies() entered
recvdoc(): entered
SetError() #0:
recvdoc(): Receiving 915 bytes.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>400 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: zWkdS1Ah4XxWdGfz8bPp43hv-YypG3qskpMgDs74_wD4dbJ780MZBw==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML>
SetError() #13: HTTP/1.1 400 Bad Request
http_close(): entered


RPG code:

//*---------------------------------------------------------------
//*‚MAIN
//*---------------------------------------------------------------
// *entry/entry parameters
dcl-pi *n;
end-pi;

//..consume REST Api.
//...testing
http_debug(*on:'/tmp/uhc_api.txt');
gPayerId = '12345678';
gTin = '12345678';
gClaimNumber = '12345678';
gBearerToken = '';
gContentType = 'text/plain';

getClaimDetailDS.transactionId = '11111-11111-11111-11111';
getDocListDs.documentList(1).docTypeCode = 'OT';
getDocListDs.documentList(1).docTypeDesc = 'Support Data for Claim';
getClaimActionDS.actions(1).ticketInfo(1).ticketNumber = 'PIQ-12345678';
//...testing

gUrl = 'https://apimarketplace.uhcprovider.com/ ... hment/v1.0?';
//..encode and add the parameters to the URL.
gvEncoder = http_url_encoder_new();
http_url_encoder_addvar( gvEncoder
: 'tin'
: %addr(gTin)
: %len(%trimr(gTin)) );

http_url_encoder_addvar( gvEncoder
: 'transactionId'
: %addr(getClaimDetailDS.transactionId)
: %len(%trimr(getClaimDetailDS.transactionId)) );

http_url_encoder_addvar( gvEncoder
: 'docTypeCode'
: %addr(getDocListDs.documentList(1).docTypeCode)
: %len(%trimr(getDocListDs.documentList(1).docTypeCode)) );

http_url_encoder_addvar( gvEncoder
: 'docTypeDesc'
: %addr(getDocListDs.documentList(1).docTypeDesc)
: %len(%trimr(getDocListDs.documentList(1).docTypeDesc)) );

http_url_encoder_addvar( gvEncoder
: 'ticketNumber'
: %addr(getClaimActionDS.actions(1).ticketInfo(1).ticketNumber)
: %len(%trimr(
getClaimActionDS.actions(1).ticketInfo(1).ticketNumber))
);

gUrl = %trim(gUrl) + http_url_encoder_getstr(gvEncoder);
http_url_encoder_free(gvEncoder);
http_xproc( HTTP_POINT_ADDL_HEADER
: %paddr(HeaderProc) );

// RequestStr = jsonData ;
// ResponseStr = http_string('POST' : gUrl : RequestStr : 'application/json') ;
//Set CCSID
// http_SetCCSIDs(1208:0);
// HTTP_SetFileCCSID(819) ;

rc = HTTP_req('POST' : %trim(gUrl) : *omit : ResponseStr
:'/tmp/UHC API Marketplace pdf.pdf'
: *omit
: 'multipart/form-data'
) ;


*inlr = *on;
return ;

Re: 400 ERROR - Bad Request

Posted: Tue May 10, 2022 9:09 pm
by Scott Klement
I'm not familiar with this API, but... you are telling it that the data is in multipart/form-data format, but you aren't encoding it in that format -- you're just sending the raw PDF.

Does the API expect it in that format? If so, use the multipart form-data encoder in HTTPAPI to encode it.

Or, does the API expect the raw PDF? If so, change the content-type to application/pdf or whatever makes sense.

Re: 400 ERROR - Bad Request

Posted: Wed May 11, 2022 2:38 am
by sgonchigar
Thank you for the quick response Scott. Trying to figure. API documentation/swagger is not clear on that. When I run from python, I see this

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): apimarketplace.uhcprovider.com:443
send: b'POST /Claims/api/attachment/v1.0?tin=12345678&docTypeCode=OZ&docTypeDesc=Support+Data+for+Claim&transactionId=11111-11111
-11111-11111&ticketNumber=PIQ-12345678 HTTP/1.1\r\nHost: apimarketplace.uhcprovider.com\r\nUser-Agent: python-requests/2.25.1\r\n
Accept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nAuthorization: \r\nenv: sandbox\r\nContent-Type: text/
plain\r\nContent-Length: 113395\r\n\r\n'
send: b'--652fc25afb2a624dd0cf0041b77ce105\r\nContent-Disposition: form-data; name="file"; filename="/tmp/UHC AP
I Marketplace pdf.pdf"\r\n\r\n%PDF-1.4\n%\xd3\xeb\xe9\xe1\n1 0 obj\n<</Creator (Mozilla/5.0 \\(Windows NT 10.0; Win64; x64\\) App
leWebKit/537.36 \\(KHTML, like Gecko\\) Chrome/101.0.4951.54 Safari/537.36)\n/Producer (Skia/PDF m101)\n/CreationDate (D:20220509
025312+00\'00\')\n/ModDate (D:20220509025312+00\'00\')>>\nendobj\n3 0 obj\n<</ca 1\n/BM /Normal>>\nendobj\n5 0 obj\n<</Type /XObj
ect\n/Subtype /Image\n/Width 2348\n/Height 159\n/ColorSpace /DeviceGray\n/BitsPerComponent 8\n/Filter /DCTDecode\n/ColorTransform
0\n/Length 7482>> stream\n\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x10\x0b\x0c\x0e\
x0c\n\x10\x0e\r\x0e\x12\x11\x10\x13\x18(\x1a\x18\x16\x16\x181#%\x1d(:3=<9387@H\\N@DWE78PmQW_bghg>Mqypdx\\egc\xff\xc0\x00\x0b\x08\
x00\x9f\t,\x01\x01\x11\x00\xff\xc4\x00\x1b\x00\x01\x00\x02\x03\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x06\x01\x0
2\x04\x03\x07\xff\xc4\x00&\x10\x01\x00\x01\x04\x02\x03\x00\x03\x01\x01\x01\x01\x01\x00\x00\x00\x00\x01\x02\x12\x15Q\x11\x14\x13Rb
\x0512\x04\x03!A"\xff\xda\x00\x08\x01\x01\x00\x00?\x00\xf9\xf9\xc36\xcb6\xc9d\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96
Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\x96Id\
----------continuation of PDF data--------------------------
ntrailer\n<</Size 63\n/Root 38 0 R\n/Info 1 0 R>>\nstartxref\n111872\n%%EOF\r\n--652fc25afb2a624dd0cf0041b77ce105--\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Type: application/json
header: Content-Length: 71
header: Connection: keep-alive
header: Request-Context: appId=cid-v1:cadad842-5962-43b8-8f7c-e71f005047d0
header: X-Azure-Ref: 0sR17YgAAAACHIbfFILJZRoQRyoEhM9ASREZXMzExMDAwMTAxMDMzADViNzY0ZjQ1LTM3ZTYtNDExZi04YTdiLWJlYmQ1Yzg2MDFkNQ==
header: Date: Wed, 11 May 2022 02:21:37 GMT
header: X-Cache: Miss from cloudfront
header: Via: 1.1 b02b0d001663966fda1c9117bcb43836.cloudfront.net (CloudFront)
header: X-Amz-Cf-Pop: DFW55-C3
header: X-Amz-Cf-Id: EQINbkzeZs0Wr7IUFl07ig5wf3aENnnZxSxxBsdiG2YYxhhF5WRUOw==
DEBUG:urllib3.connectionpool:https://apimarketplace.uhcprovider.com:443 "POST /Claims/api/attachment/v1.0?tin=12345678&docTypeCod
e=OZ&docTypeDesc=Support+Data+for+Claim&transactionId=11111-11111-11111-11111&ticketNumber=PIQ-12345678 HTTP/1.1" 200 71
{
"attachmentId": "1234-1111-46a9-111-11117Cu_prov_attch_2021-11"
}
status code 200

Re: 400 ERROR - Bad Request

Posted: Wed May 11, 2022 7:34 am
by Scott Klement
Yeah, the data is encoded as multipart/form-data. (Though, it's sending a content-type of text/plain which is weird -- but maybe the server accepts it.)

Use HTTPAPI's multipart/form-data encoder.

Re: 400 ERROR - Bad Request

Posted: Wed May 11, 2022 6:35 pm
by sgonchigar
THANK YOU Scott!. Followed Example7 and it worked. Below is the working code.

Code: Select all

//*---------------------------------------------------------------
      //*‚MAIN
      //*---------------------------------------------------------------
      // *entry/entry parameters
       dcl-pi *n;
       end-pi;      
       //..consume REST Api.
        //...testing
       http_debug(*on:'/tmp/uhc_api.txt');
       gPayerId = '12345678';
       gTin = '12345678';
       gClaimNumber = '12345678';
       gBearerToken = '';
       gENVIRONMENT = 'sandbox';

       getClaimDetailDS.transactionId = '11111-11111-11111-11111';
       getDocListDs.documentList(1).docTypeCode = 'OT';
       getDocListDs.documentList(1).docTypeDesc = 'Support Data for Claim';
       getClaimActionDS.actions(1).ticketInfo(1).ticketNumber = 'PIQ-12345678';
       //...testing

       gUrl = 'https://apimarketplace.uhcprovider.com/Claims/api/attachment/v1.0?';

       //..encode and add the parameters to the URL.
       gvEncoder = http_url_encoder_new();
       http_url_encoder_addvar( gvEncoder
                              : 'tin'
                              : %addr(gTin)
                              : %len(%trimr(gTin)) );

       http_url_encoder_addvar( gvEncoder
                              : 'transactionId'
                              : %addr(getClaimDetailDS.transactionId)
                              : %len(%trimr(getClaimDetailDS.transactionId)) );

       http_url_encoder_addvar( gvEncoder
                              : 'docTypeCode'
                              : %addr(getDocListDs.documentList(1).docTypeCode)
                              : %len(%trimr(getDocListDs.documentList(1).docTypeCode)) );

       http_url_encoder_addvar( gvEncoder
                              : 'docTypeDesc'
                              : %addr(getDocListDs.documentList(1).docTypeDesc)
                              : %len(%trimr(getDocListDs.documentList(1).docTypeDesc)) );

       http_url_encoder_addvar( gvEncoder
                              : 'ticketNumber'
                              : %addr(getClaimActionDS.actions(1).ticketInfo(1).ticketNumber)
                              : %len(%trimr(
                                getClaimActionDS.actions(1).ticketInfo(1).ticketNumber))
                              );

       gUrl = %trim(gUrl) + http_url_encoder_getstr(gvEncoder);
       http_url_encoder_free(gvEncoder);

       http_xproc( HTTP_POINT_ADDL_HEADER
              : %paddr(HeaderProc) );

  
       tempFile = http_tempfile();
       enc = http_mfd_encoder_open( tempFile : gContentType );
       if (enc = *NULL);
         msg = http_error();
         dsply msg;
         return;
       endif;

       http_mfd_encoder_addstmf( enc
                               : 'file'
                               : '/temp/UHC API Marketplace pdf.pdf'
                               : 'application/octet-stream');

       http_mfd_encoder_close( enc );

       http_setOption('timeout': '300');   // timeout = 5 minutes

       rc = HTTP_req('POST' : %trim(gUrl) : *omit : ResponseStr
                     :tempFile
                     : *omit
                     : gContentType
                    ) ;

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

       unlink(tempFile);

       *inlr = *on;
       return ;

      //-----------------------------------------------------------------------
      //  REST api Headers:
      //-----------------------------------------------------------------------
      dcl-proc HeaderProc ;
      dcl-pi *n;
         header                    varchar(32767);
       end-pi;
       header =  'Authorization:' + %trim(gBearerToken)          + CRLF
               + 'env:'           + %trim(gENVIRONMENT)   + CRLF
               + 'Accept-Encoding:gzip,deflate'   + CRLF
               + 'Accept: */*' + CRLF
               ;
      end-proc HeaderProc ;

Re: 400 ERROR - Bad Request

Posted: Fri May 13, 2022 7:36 pm
by Scott Klement
Please remove this line from your code:

Code: Select all

Accept-Encoding:gzip,deflate'   + CRLF
HTTPAPI does not support gzip or deflate encoding, so if you add something like that and communicate with a server that supports it, your data will come back compressed, and you won't be able to read it. Unless you're planning to write your own code to support that?