I get "HTTP Error 411. The request must be chunked or have a content length." when I do a post. It's working from Postman and JavaScript. But not from CRM-script. The header Content-Length is correct, but it seems that it does not "read" it.
Anyone?
From debug log:
=> Send header
POST /api/sale HTTP/1.1
Host: api.123.no
Accept: */*
Content-Type: application/json
Authorization: xxxxxxxxxxxxxxxxxxxxxxxx
Content-Length: 100
traceparent: 00-ed27b3cd060c4144a33c4a6f5e705d16-d6bbf292bd167881-01
=> Send data
{"OrderDate": "2024-09-06 13:53:13","OurRef": "Frode Folleras","CustomerNo": "12345", "Lines": [{}]}
<= Recv header
HTTP/1.1 411 Length Required
Allt Svar (6)
Hi,
If you post the same request using CRMScript to 'https://httpbin.org/post', do you also get an error then?
Late answer to you question David, but no. I don't get the error. But I see in response from 'https://httpbin.org/post' that content-length is not in the "headers" section so it seems that the header is not included in the request.
Hi Follerås, this is very interresting. Could you post a simple CRMScript example which show the issue?
Since Content-Length header is listed before the traceparent, I suspect you are trying to add the Content-Length header manually?
Hi Follerås,
You do not have to set the Host-header. HTTP class (or more likely the underlying curl library) will add that for you.
Also, the Content-Length you're adding manually should be skipped. It will also be added by curl. And in fact, it is the reason you don't get a response from httpbin.org. The leading "x", which the HTTP class removes, makes the calculated length wrong. You manually add a header saying the length should be 14 bytes, but the actual content you send is just 13 bytes. So the remote server is sitting there waiting for that last byte to come through. When it never comes you get a timeout.
It is also a lot easier to use structs for building up your JSON structure.
Struct Payload
{
String name;
Integer age;
};
Payload p;
p.name = "Blåbærsyltetøy";
p.age = 42;
HTTP http;
http.setDebugMode(true);
http.addHeader("Content-Type","application/json;charset=utf-8");
http.setOption("parameters", "?" + p.toJsonString());
http.setOption("parametersUTF8", "true");
String url = "https://httpbin.org/post";
String response = String(http.post(url));
printLine(http.getDebug());
printLine("Response: " + response);
Now, that in itself shouldn't really give that 411 error. And you probably won't get that HTTP statuscode from most servers, so I you need to test against the same server as you got the error initially.
The example posted in the beginning of the thread had an Authorization header as well, and I suspect that if the value you put in the Authorization header contains a newline, and then you add your custom Content-Length header afterwards, it will appear to the server as there is no Content-Length.
Example:
Struct Payload
{
String name;
Integer age;
};
Payload p;
p.name = "Blåbærsyltetøy";
p.age = 42;
HTTP http;
http.setDebugMode(true);
http.addHeader("Content-Type","application/json;charset=utf-8");
http.addHeader("Authorization", "foo\r\n"); // Notice trailing newline
http.addHeader("Content-Length", p.toJsonString().getLength().toString()); // ... and manually adding Content-Length afterwards
http.setOption("parameters", "?" + p.toJsonString());
http.setOption("parametersUTF8", "true");
String url = "https://httpbin.org/post";
String response = String(http.post(url));
printLine(http.getDebug());
printLine("Response: " + response);
In that case the server will see this request:
POST /post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Type: application/json;charset=utf-8
Authorization: foo
Content-Length: 36
traceparent: 00-1e52dcfe166c4d5fbdeca16d4fd67cc7-1e876b41b5d2fe14-01
Notice the newline before Content-Length, which makes it not part of the headers.
Could something like that be the reason?