<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Posting FormData to external REST API in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541108#M4701702</link>
    <description>&lt;P&gt;Hi Nico,&lt;/P&gt;&lt;P&gt;could you try to set the "content-type" header manually with .send?&lt;/P&gt;&lt;P&gt;The default is "application/json" which does not fit your content.&lt;/P&gt;&lt;P&gt;Please also include the error message as it might give a hint what's causing the issue.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Johannes&lt;/P&gt;</description>
    <pubDate>Wed, 29 Dec 2021 12:19:26 GMT</pubDate>
    <dc:creator>johannesvogel</dc:creator>
    <dc:date>2021-12-29T12:19:26Z</dc:date>
    <item>
      <title>Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaq-p/12541107</link>
      <description>&lt;P&gt;Hi CAP Community,&lt;/P&gt;
  &lt;P&gt;I'm trying to Post a file as &lt;A href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData"&gt;FormData&lt;/A&gt; to an external rest API. I'm following the documentation here: &lt;A href="https://cap.cloud.sap/docs/node.js/services#srv-send"&gt;https://cap.cloud.sap/docs/node.js/services#srv-send&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;Using &lt;A href="https://www.npmjs.com/package/node-fetch"&gt;fetch&lt;/A&gt; it was pretty straight forward and is working fine:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;        const imageBuffer = fs.readFileSync('./testfiles/filename.png')

        const form = new FormData()
        form.append('file', imageBuffer, {
            contentType: 'text/plain',
            name: 'file',
            filename: 'filename.png'
        })

        const headers = {
            'Authorization': 'Basic ' + btoa(process.env.LOGIN)
        }

        const url = process.env.URL + '/my/api/path' 

        const response = await fetch(url, {
            method: 'POST',
            headers: headers,
            body: form,
        })

        if (!response.ok) throw new Error(`unexpected response ${response.statusText}`)  
        //hurray, it works&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;Next I added the api connection information to the package.json:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;  "cds": {
    "requires": {
      "myAPI": {
        "kind": "rest",
        "credentials": {
          "url": ...,
          "authentication": "BasicAuthentication",
          "username": ...,
          "password": ...,
          "requestTimeout": 30000
        }
      }
   }
 }&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;and tried to archive the same using cds.connect and cds.post/cds.send&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;    this.on('submitFile', async req =&amp;gt; {
        const imageBuffer = fs.readFileSync('./testfiles/filename.png')

        const form = new FormData()
        form.append('file', imageBuffer, {
           contentType: 'text/plain',
           name: 'file',
           filename: 'filename.png'
        })

        try {
            const myAPI = await cds.connect.to('myAPI')

            //Does not work
            const response1 = await myAPI.post('/my/api/path', form)

            //Does not work either
            const response2 = await myAPI.send({
               method: 'POST',
               path: '/my/api/path',
               data: form
            })
       } catch (err) {
            console.log("Error message: " + err.message) //Error during request to remote service: Request failed with status code 404
        }
    })&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;but no success. Does anyone has an idea what I'm doing wrong? Is FormData not supported as payload?&lt;/P&gt;
  &lt;P&gt;Thanks &amp;amp; Regards&lt;BR /&gt;Nico&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2021 10:23:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaq-p/12541107</guid>
      <dc:creator>nicorunge</dc:creator>
      <dc:date>2021-12-23T10:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541108#M4701702</link>
      <description>&lt;P&gt;Hi Nico,&lt;/P&gt;&lt;P&gt;could you try to set the "content-type" header manually with .send?&lt;/P&gt;&lt;P&gt;The default is "application/json" which does not fit your content.&lt;/P&gt;&lt;P&gt;Please also include the error message as it might give a hint what's causing the issue.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Johannes&lt;/P&gt;</description>
      <pubDate>Wed, 29 Dec 2021 12:19:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541108#M4701702</guid>
      <dc:creator>johannesvogel</dc:creator>
      <dc:date>2021-12-29T12:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541109#M4701703</link>
      <description>&lt;P&gt;Hi Johannes,&lt;/P&gt;&lt;P&gt;I tried adding the content-type like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;            &lt;BR /&gt;            const response2 = await myAPI.tx(req).send({&lt;BR /&gt;                method: 'POST',&lt;BR /&gt;                path: '/my/api/path',&lt;BR /&gt;                data: form,&lt;BR /&gt;                headers: {&lt;BR /&gt;                    'Content-Type': 'multipart/form-data'&lt;BR /&gt;                }&lt;BR /&gt;            })&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it's still returning the timeout message: "Error during request to remote service: Request failed with status code 408"&lt;/P&gt;&lt;P&gt;Here's the full output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;[cds] - POST /browse/submitFile&lt;BR /&gt;[cds] - connect to converter &amp;gt; rest {&lt;BR /&gt;  url: 'XXXXXXXXXXXXXXXXXXX',&lt;BR /&gt;  requestTimeout: 30000,&lt;BR /&gt;  authentication: '...',&lt;BR /&gt;  username: '...',&lt;BR /&gt;  password: '...'&lt;BR /&gt;}&lt;BR /&gt;[remote] - Error: Error during request to remote service: Request failed with status code 408&lt;BR /&gt;    at createError (/workspaces/projects/Converter_CAP/node_modules/axios/lib/core/createError.js:16:15)&lt;BR /&gt;    at settle (/workspaces/projects/Converter_CAP/node_modules/axios/lib/core/settle.js:17:12)&lt;BR /&gt;    at IncomingMessage.handleStreamEnd (/workspaces/projects/Converter_CAP/node_modules/axios/lib/adapters/http.js:269:11)&lt;BR /&gt;    at IncomingMessage.emit (events.js:412:35)&lt;BR /&gt;    at endReadableNT (internal/streams/readable.js:1334:12)&lt;BR /&gt;    at processTicksAndRejections (internal/process/task_queues.js:82:21) {&lt;BR /&gt;  config: {&lt;BR /&gt;    url: '/my/api/path',&lt;BR /&gt;    method: 'post',&lt;BR /&gt;    proxy: false,&lt;BR /&gt;    baseURL: 'XXXXXXXXXXXXXXXXXXX',&lt;BR /&gt;    timeout: 30000,&lt;BR /&gt;    xsrfCookieName: 'XSRF-TOKEN',&lt;BR /&gt;    xsrfHeaderName: 'X-XSRF-TOKEN',&lt;BR /&gt;    maxContentLength: -1,&lt;BR /&gt;    maxBodyLength: -1&lt;BR /&gt;  },&lt;BR /&gt;  request: {&lt;BR /&gt;    method: 'POST',&lt;BR /&gt;    url: 'XXXXXXXXXXXXXXXXXXX/my/api/path',&lt;BR /&gt;    headers: {&lt;BR /&gt;      Accept: 'application/json,text/plain',&lt;BR /&gt;      'Content-Type': 'multipart/form-data',&lt;BR /&gt;      authorization: 'Basic ...',&lt;BR /&gt;      'accept-language': 'en',&lt;BR /&gt;      'content-length': 59951,&lt;BR /&gt;      'User-Agent': 'axios/0.21.4'&lt;BR /&gt;    }&lt;BR /&gt;  },&lt;BR /&gt;  response: {&lt;BR /&gt;    status: 408,&lt;BR /&gt;    statusText: 'Request Timeout',&lt;BR /&gt;    headers: {&lt;BR /&gt;      date: 'Thu, 06 Jan 2022 10:56:11 GMT',&lt;BR /&gt;      server: 'Apache/2.4.29 (Ubuntu)',&lt;BR /&gt;      'content-length': '312',&lt;BR /&gt;      connection: 'close',&lt;BR /&gt;      'content-type': 'text/html; charset=iso-8859-1'&lt;BR /&gt;    },&lt;BR /&gt;    body: '&amp;lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;title&amp;gt;408 Request Timeout&amp;lt;/title&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;h1&amp;gt;Request Timeout&amp;lt;/h1&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;p&amp;gt;Server timeout waiting for the HTTP request from the client.&amp;lt;/p&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;hr&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;address&amp;gt;Apache/2.4.29 (Ubuntu) Server at XXXXXXXXXXXXXXXXXXX Port 443&amp;lt;/address&amp;gt;\n' +&lt;BR /&gt;      '&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;\n'&lt;BR /&gt;  },&lt;BR /&gt;  isAxiosError: true,&lt;BR /&gt;  correlationId: '8c40defc-c917-4b86-9fb5-1d6c223ac59c'&lt;BR /&gt;}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;best regards&lt;BR /&gt;Nico&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 11:41:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541109#M4701703</guid>
      <dc:creator>nicorunge</dc:creator>
      <dc:date>2022-01-06T11:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541110#M4701704</link>
      <description>&lt;P&gt;Hi Nico,&lt;/P&gt;&lt;P&gt;I believe with 6.0.3 we have fixed this issue.&lt;/P&gt;&lt;P&gt;Reason was that the content-length header was calculated wrongly and hence the remote application is waiting for more data to process and finally runs in a timeout.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Johannes&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 08:55:08 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541110#M4701704</guid>
      <dc:creator>johannesvogel</dc:creator>
      <dc:date>2022-08-12T08:55:08Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541111#M4701705</link>
      <description>&lt;P&gt;Hi Nico,&lt;/P&gt;&lt;P&gt;I believe with 6.0.3 we have fixed this issue.&lt;/P&gt;&lt;P&gt;Reason was that the content-length header was calculated wrongly and hence the remote application is waiting for more data to process and finally runs in a timeout.&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Johannes&lt;/P&gt;</description>
      <pubDate>Fri, 12 Aug 2022 08:55:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541111#M4701705</guid>
      <dc:creator>johannesvogel</dc:creator>
      <dc:date>2022-08-12T08:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541112#M4701706</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;johannesvogel&lt;/SPAN&gt;, &lt;/P&gt;&lt;P&gt;finally had the time to test and can confirm it works now.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;            const response = await myAPI.send({&lt;BR /&gt;                method: 'POST',&lt;BR /&gt;                path: apiPath,&lt;BR /&gt;                data: form,&lt;BR /&gt;                headers: {&lt;BR /&gt;                    'Content-Type': 'multipart/form-data',&lt;BR /&gt;                    'Accept': 'multipart/mixed'&lt;BR /&gt;                }&lt;BR /&gt;            })&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But this leads me to another question. Is there a way to access the response header? Right now, the response only contains the responded data. But as it is &lt;STRONG&gt;multipart/mixed&lt;/STRONG&gt;, I have to parse the response using the returned boundary, which is included in the content-type.&lt;/P&gt;&lt;P&gt;Content-Type: multipart/mixed;boundary=Boundary_1229_117813444_54355453674;charset=UTF-8&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Using fetch, I could simply use &lt;STRONG&gt;response.headers.get('content-type')&lt;/STRONG&gt;, to get the boundary info. But how to access the headers when using CAP, as &lt;STRONG&gt;response.headers&lt;/STRONG&gt; is undefined? Why is it not returning the plain axios response object?&lt;BR /&gt;&lt;/P&gt;&lt;P&gt; Thanks &amp;amp; regards&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Nico&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Feb 2023 16:16:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541112#M4701706</guid>
      <dc:creator>nicorunge</dc:creator>
      <dc:date>2023-02-20T16:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541113#M4701707</link>
      <description>&lt;P&gt;Hi  &lt;SPAN class="mention-scrubbed"&gt;johannesvogel&lt;/SPAN&gt;   it seems like the problem with the timeout still exist in version  6.0.3, in our case we solved it calculating the content-length header and passing it to the send.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;  var bodyFormData = new FormData();
  bodyFormData.append("var1", var1);
  bodyFormData.append("var2", var2);
  const myAPI = await cds.connect.to(destinationName);
    let response;
    try {
    response = await myAPI.send({
          method: 'POST',
          path: '/someURL',
          data: bodyFormData,
          headers: {
            'Content-Type': 'multipart/form-data',
            'Content-Length': bodyFormData.getLengthSync()
          }
      })
	  }
	  catch (error) {
        throw new Error(
        "MyAPI err: " + error.message
        );
     }&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;	  &lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 09:29:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541113#M4701707</guid>
      <dc:creator>former_member825988</dc:creator>
      <dc:date>2023-02-28T09:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: Posting FormData to external REST API</title>
      <link>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541114#M4701708</link>
      <description>&lt;P&gt;Looks like the issue is not fixed. I'm having the same situation and I plan to send a stream. Feels like there is no way to calculate content length for a stream. I'm using version &amp;gt;7&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2023 02:05:41 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/posting-formdata-to-external-rest-api/qaa-p/12541114#M4701708</guid>
      <dc:creator>rui_jin</dc:creator>
      <dc:date>2023-08-25T02:05:41Z</dc:date>
    </item>
  </channel>
</rss>

