needle
needle copied to clipboard
Lexical error in multipart request
I am trying to perform a multipart PUT request to CouchDB, but am receiving the following error.
[Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] 'PUT' /couch-recliner-test/fake-id {1,1} from "127.0.0.1"
Headers: [{'Accept',"application/json"},
{'Connection',"close"},
{'Content-Length',"665"},
{'Content-Type',"multipart/form-data; boundary=--------------------NODENEEDLEHTTPCLIENT"},
{'Host',"localhost:5984"},
{'User-Agent',"Needle/1.5.0 (Node.js v7.7.3; linux x64)"}]
[Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] OAuth Params: []
[Sun, 26 Mar 2017 16:21:04 GMT] [error] [<0.6514.2>] attempted upload of invalid JSON (set log_level to debug to log it)
[Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] Invalid JSON: {{error,
{1,
"lexical error: malformed number, a digit is required after the minus sign.\n"}},
<<"----------------------NODENEEDLEHTTPCLIENT\r\nContent-Disposition: form-data; name=\"document_body\"\r\nContent-Type: application/json\r\n\r\n{\"test\":\"data\",\"hello\":22,\"deep\":{\"more\":\"hii\",\"array\":[22,33,145]},\"array\":[\"some\",\"text\",\"here\"],\"description\":\"A longer bit of text with perhaps a line break in it.\\nRight here.\",\"bool\":true,\"_attachments\":{\"fake-attachment.txt\":{\"follows\":true,\"content_type\":\"text/html\",\"length\":46}}}\r\n----------------------NODENEEDLEHTTPCLIENT\r\nContent-Disposition: form-data; name=\"file_0\"; filename=\"fake-attachment.txt\"\r\nContent-Type: text/html\r\n\r\nThis is an example attachment containing text.\r\n----------------------NODENEEDLEHTTPCLIENT--">>}
[Sun, 26 Mar 2017 16:21:04 GMT] [info] [<0.6514.2>] 127.0.0.1 - - PUT /couch-recliner-test/fake-id 400
[Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] httpd 400 error response:
{"error":"bad_request","reason":"invalid_json"}
The request looks approximately like this.
const mydata = {
document_body: {
content_type: 'application/json',
value: '{"test":"data","hello":22,"deep":{"more":"hii","array":[22,33,145]},"array":["some","text","here"],"description":"A longer bit of text with perhaps a line break in it.\\nRight here.","bool":true,"_attachments":{"fake-attachment.txt":{"follows":true,"content_type":"text/html","length":46}}}'
},
file_0: {
content_type: 'text/html',
filename: 'fake-attachment.txt',
buffer: mybuffer
}
};
needle.request('PUT', url, mydata, { multipart: true, json: true }, callback);
The key part of the error seems to be.
"lexical error: malformed number, a digit is required after the minus sign.\n"
But I am unsure what it means.
Does it need to be both multipart AND json at the same time? Try without one or the other. On Mar 26, 2017 9:32 AM, "Kequc" [email protected] wrote:
I am trying to perform a multipart PUT request to CouchDB, but am receiving the following error.
[Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] 'PUT' /couch-recliner-test/fake-id {1,1} from "127.0.0.1" Headers: [{'Accept',"application/json"}, {'Connection',"close"}, {'Content-Length',"665"}, {'Content-Type',"multipart/form-data; boundary=--------------------NODENEEDLEHTTPCLIENT"}, {'Host',"localhost:5984"}, {'User-Agent',"Needle/1.5.0 (Node.js v7.7.3; linux x64)"}] [Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] OAuth Params: [] [Sun, 26 Mar 2017 16:21:04 GMT] [error] [<0.6514.2>] attempted upload of invalid JSON (set log_level to debug to log it) [Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] Invalid JSON: {{error, {1, "lexical error: malformed number, a digit is required after the minus sign.\n"}}, <<"----------------------NODENEEDLEHTTPCLIENT\r\nContent-Disposition: form-data; name="document_body"\r\nContent-Type: application/json\r\n\r\n{"test":"data","hello":22,"deep":{"more":"hii","array":[22,33,145]},"array":["some","text","here"],"description":"A longer bit of text with perhaps a line break in it.\nRight here.","bool":true,"_attachments":{"fake-attachment.txt":{"follows":true,"content_type":"text/html","length":46}}}\r\n----------------------NODENEEDLEHTTPCLIENT\r\nContent-Disposition: form-data; name="file_0"; filename="fake-attachment.txt"\r\nContent-Type: text/html\r\n\r\nThis is an example attachment containing text.\r\n----------------------NODENEEDLEHTTPCLIENT--">>} [Sun, 26 Mar 2017 16:21:04 GMT] [info] [<0.6514.2>] 127.0.0.1 - - PUT /couch-recliner-test/fake-id 400 [Sun, 26 Mar 2017 16:21:04 GMT] [debug] [<0.6514.2>] httpd 400 error response: {"error":"bad_request","reason":"invalid_json"}
The request looks approximately like this.
const mydata = { document_body: { content_type: 'application/json', value: '{"test":"data","hello":22,"deep":{"more":"hii","array":[22,33,145]},"array":["some","text","here"],"description":"A longer bit of text with perhaps a line break in it.\nRight here.","bool":true,"_attachments":{"fake-attachment.txt":{"follows":true,"content_type":"text/html","length":46}}}' }, file_0: { content_type: 'text/html', filename: 'fake-attachment.txt', buffer: mybuffer } };
needle.request('PUT', url, mydata, { multipart: true, json: true }, callback);
The key part of the error seems to be.
"lexical error: malformed number, a digit is required after the minus sign.\n"
But I am unsure what it means.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tomas/needle/issues/209, or mute the thread https://github.com/notifications/unsubscribe-auth/AAApThSzMXdsaVZmipqDMitookWHV6SKks5rppMjgaJpZM4MpexQ .
Expected behaviour, I suppose would be that the data is sent in multipart and accepts a json response.
I'm not sure I understand. Did it work? What behaviour were you expecting instead?
The next issue I was coming up to was that CouchDB seems to expect a specific order to the multipart request. I am not sure I'd be able to ensure that from a key value pair. So I had to switch away to the much larger request library.
I'm so sorry.
With regard to expected behaviour, I didn't expect the error and expected what I laid out in my second comment.
No need to say I'm sorry. We're all friends here. ;)
How should I fire a CouchDB instance in order to try out your code?
On Tue, Mar 28, 2017 at 9:25 AM, Kequc [email protected] wrote:
The next issue I was coming up to was that CouchDB seems to expect a specific order to the multipart request. So I am not sure I'd be able to ensure that from a key value pair. So I had to switch away to the much larger request library.
I'm so sorry.
With regard to expected behaviour, I didn't expect the error and expected what I laid out in my second comment.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tomas/needle/issues/209#issuecomment-289825682, or mute the thread https://github.com/notifications/unsubscribe-auth/AAApTiZGiZGeSrqTHgIz1X1dVF-9XZyDks5rqTRtgaJpZM4MpexQ .
-- Fork Ltd. forkhq.com
I suppose just download and install it. By default the db instance runs at http://127.0.0.1:5984
https://couchdb.apache.org/
I finally took the time to set up CouchDB (v1.6.0) and try your code, and after some testing I found out:
a) that yes, your example does render a { error: 'bad_request', reason: 'invalid_json' } response,
b) that if removing the multipart: true option works (at least it does create a new record),
c) that replacing 'multipart/form-data' with 'multipart/related' seems to be the right way to get it working, but for some reason when using that header the request never finishes. This seems to be the reason why.