LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2017, 01:26 AM   #1
JustABanana
LQ Newbie
 
Registered: May 2017
Posts: 2

Rep: Reputation: Disabled
Chunked HTTP POST without curl


Hi.


I've been trying to do HTTP POST for large file for some time now. Apparently curl libraries are so dominant, that there's quite little examples how to do it _without curl_.

What I've understood is that you need to add block size string in hex form to start of each block followed with CRLF. Then the payload.

Code:
const static char *synth_header = "POST /v1/avs/speechrecognizer/recognize HTTP/1.1\r\n\
Host: access-alexa-na.amazon.com\r\n\
Authorization: Bearer Atza|Iw...\r\n\
Content-Type: multipart/form-data; boundary=BOUNDARY1234\r\n\
Expect: 100-continue\r\n\
Transfer-Encoding: chunked\r\n\r\n";

const static char *synth_payload = "--BOUNDARY1234\r\n\
Content-Disposition: form-data; name=\"metadata\"\r\n\
Content-Type: application/json; charset=UTF-8\r\n\r\n\
{\"messageHeader\":{},\"messageBody\": {\"profile\": \"alexa-close-talk\",\"locale\": \"en-us\",\"format\": \"audio/L16; rate=16000; channels=1\"}}\r\n\r\n\
--BOUNDARY1234\r\n\
Content-Disposition: form-data; name=\"audio\"\r\n\
Content-Type: audio/L16; rate=16000; channels=1\r\n\r\n";

const static char* boundary_term = "\r\n--BOUNDARY1234--\r\n";

const static char* end_chunk = "0\r\n\r\n";
I first send the synth header and wait for the 100-continue response. Then I send the synth_payload (with the chunk size). After that I'm sending first real audio blob as payload (again with chunk size). After x amount of blobs I'm sending the end_end chunk.

The code is based on openssl socket example (I can post more code samples, but it's rather simple, but longish). I'm trying to reproduce this alexa curl example without curl:
https://miguelmota.com/blog/alexa-vo...ice-with-curl/

Problem is that every time I get 100-continue OK, but after sending the chunks I just get 400 bad request without any further info. So the authentication should be OK. I'm quite sure I'm messing up the chunking somehow. And system I'm using this can only do 1k transfers at the time, so I have to do some sort of chunking.

Curl response:
Code:
* ALPN, server accepted to use http/1.1
> POST /v1/avs/speechrecognizer/recognize HTTP/1.1
> Host: access-alexa-na.amazon.com
> User-Agent: curl/7.47.0
> Accept: */*
> Authorization: Bearer Atza|I...
> Content-Type: multipart/form-data; boundary=BOUNDARY1234
> Content-Length: 50385
> Expect: 100-continue
> 
< HTTP/1.1 100 Continue
} [16384 bytes data]
* We are completely uploaded and fine
100 50385    0     0  100 50385      0  26405  0:00:01  0:00:01 --:--:-- 26393< HTTP/1.1 200 OK
< Server: Server
< Date: Tue, 09 May 2017 13:38:43 GMT
< Content-Type: multipart/related; boundary=873c15a5-9d98-4e35-9e50-070af24e6b2c; start=metadata.1494337123714; type="application/json"
< Transfer-Encoding: chunked
< Connection: keep-alive
< x-amzn-RequestId: d0fff283-34bc-11e7-822f-ef09982f8b0b
< Vary: Accept-Encoding,User-Agent
< 
{ [405 bytes data]
100 63265    0 12880  100 50385   4371  17098  0:00:02  0:00:02 --:--:-- 17097
 
Old 05-10-2017, 02:21 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It doesn't have to be chunked; if you know the size, send 'Content-Length: 11223344'
Also the Expect/Continue message are optional, they can be safely dropped.
 
Old 05-10-2017, 04:03 AM   #3
JustABanana
LQ Newbie
 
Registered: May 2017
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
It doesn't have to be chunked; if you know the size, send 'Content-Length: 11223344'
Also the Expect/Continue message are optional, they can be safely dropped.
This is what I've also understood. However if I would like to do the chunking how would that go? My test doesn't seem to be working in that case.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] cURL utility to post http payload to invoke the API boby.kumar Linux - Newbie 6 11-02-2015 08:48 AM
php curl http post request submit with a secure connection conflicker Programming 0 06-13-2012 12:31 PM
[SOLVED] cURL POST data command from shell returns HTTP Error Code 408 fyndr Linux - Software 3 03-04-2011 09:17 AM
curl and wget http post williebens Linux - Newbie 4 10-11-2010 11:03 PM
Curl...HTTP disable slapt-get Acron_0248 Slackware 5 05-08-2006 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration