Skip to content

Instantly share code, notes, and snippets.

@gustawdaniel
Created May 19, 2017 21:28
Show Gist options
  • Save gustawdaniel/eca58c4e4881648064487bc27925c698 to your computer and use it in GitHub Desktop.
Save gustawdaniel/eca58c4e4881648064487bc27925c698 to your computer and use it in GitHub Desktop.
Presentation of sending multiple files in POST

Firstly install httpie

sudo apt-get install httpie

Next in firs console run server

php -S 0.0.0.0:8000

In second consoel run sender

bash sender.sh

You should see:

Processing file1.json
POST /app.php HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 14
Content-Type: application/json
Host: 127.0.0.1:8000
User-Agent: HTTPie/0.9.2

{
    "a": "b"
}

HTTP/1.1 200 OK
Connection: close
Content-type: text/html; charset=UTF-8
Host: 127.0.0.1:8000
X-Powered-By: PHP/7.0.15-0ubuntu0.16.04.4

{
  "a": "b"
}

Processing file2.json
POST /app.php HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 13
Content-Type: application/json
Host: 127.0.0.1:8000
User-Agent: HTTPie/0.9.2

{
    "d": "e"
}

HTTP/1.1 200 OK
Connection: close
Content-type: text/html; charset=UTF-8
Host: 127.0.0.1:8000
X-Powered-By: PHP/7.0.15-0ubuntu0.16.04.4

{
  "d":"e"
}

There are two requests and two responses.

{
"a": "b"
}
#!/usr/bin/env bash
# We iterating over all json files
for f in file1.json file2.json
do
echo "Processing $f"
# content of any file is given to command http, that you can install by sudo apt-get install httpie
cat "$f" | http -v POST 127.0.0.1:8000/server.php Content-Type:application/json
done
<?php
// this file shows incoming body for request, it is kind of mirror server for debug and tests
echo file_get_contents('php://input');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment