Posting with file using netcat (nc)

Pushsafer make it easy and safe to get push-notifications in real time on your Android or Windows mobile device, iPhone, iPad and Desktop!

Mit Pushsafer kann man in Echtzeit, einfach und sicher, Push-Benachrichtigungen auf sein iPhone, iPad, Android oder Windows Gerät sowie auf den Desktop senden lassen!

======================
Google Play
App Store
Windows Store
Homepage
nick9072

Posting with file using netcat (nc)

Postby nick9072 » 20. Jul 2020, 15:31

Hello, I know pushsafer has done an excellent job documenting the various ways to interface to the api. Some of us are working with very primitive Iot platforms, so we need to have very basic examples of how to send the proper string. And http is very fussy and it does not give a lot of feedback when an error occurs. The "cleanest" way to send data over tcp is often netcat/nc. I am trying to post data using netcat so that I can use it as a template for sending data using an Iot platform that only has basic print statements. So far, no luck. I am wondering if you could look at the file I am trying to send and see if there are any basic mistakes I am making. Also, if pushsafer wanted to create a netcat example I think that might be helpful to a lot of people trying to use the service from primitive platforms.

the command I am using is:
cat post.txt | nc 212.83.36.91 80
Here is the file I am sending:

Code: Select all

POST /api HTTP/1.1
Host: www.pushsafer.com
User-Agent: arduino 1.11
Accept: */*
Content-Length: 1082
Content-Type: multipart/form-data; boundary=------------------------ebaccb7e077deebc
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="t"

WARNING
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="k"

**********************
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="m"

Your stuff is being stolen!
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="s"

sound
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="v"

vibration
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="i"

icon
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="c"

iconcolor
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="p"

picture
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="d"

25258
--------------------------ebaccb7e077deebc
Content-Disposition: form-data; name="u"

url
----------------------------ebaccb7e077deebc--
Also, here is the hexdump of post.txt:

Code: Select all

00000000  50 4f 53 54 20 2f 61 70  69 20 48 54 54 50 2f 31  |POST /api HTTP/1|
00000010  2e 31 0d 0a 48 6f 73 74  3a 20 77 77 77 2e 70 75  |.1..Host: www.pu|
00000020  73 68 73 61 66 65 72 2e  63 6f 6d 0d 0a 55 73 65  |shsafer.com..Use|
00000030  72 2d 41 67 65 6e 74 3a  20 61 72 64 75 69 6e 6f  |r-Agent: arduino|
00000040  20 31 2e 31 31 0d 0a 41  63 63 65 70 74 3a 20 2a  | 1.11..Accept: *|
00000050  2f 2a 0d 0a 43 6f 6e 74  65 6e 74 2d 4c 65 6e 67  |/*..Content-Leng|
00000060  74 68 3a 20 31 30 38 32  0d 0a 43 6f 6e 74 65 6e  |th: 1082..Conten|
00000070  74 2d 54 79 70 65 3a 20  6d 75 6c 74 69 70 61 72  |t-Type: multipar|
00000080  74 2f 66 6f 72 6d 2d 64  61 74 61 3b 20 62 6f 75  |t/form-data; bou|
00000090  6e 64 61 72 79 3d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |ndary=----------|
000000a0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 65 62  |--------------eb|
000000b0  61 63 63 62 37 65 30 37  37 64 65 65 62 63 0d 0a  |accb7e077deebc..|
000000c0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000000d0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 65 62 61 63 63 62  |----------ebaccb|
000000e0  37 65 30 37 37 64 65 65  62 63 0d 0a 43 6f 6e 74  |7e077deebc..Cont|
000000f0  65 6e 74 2d 44 69 73 70  6f 73 69 74 69 6f 6e 3a  |ent-Disposition:|
00000100  20 66 6f 72 6d 2d 64 61  74 61 3b 20 6e 61 6d 65  | form-data; name|
00000110  3d 22 74 22 0d 0a 0d 0a  57 41 52 4e 49 4e 47 0d  |="t"....WARNING.|
00000120  0a 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |.---------------|
00000130  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 65 62 61 63 63  |-----------ebacc|
00000140  62 37 65 30 37 37 64 65  65 62 63 0d 0a 43 6f 6e  |b7e077deebc..Con|
00000150  74 65 6e 74 2d 44 69 73  70 6f 73 69 74 69 6f 6e  |tent-Disposition|
00000160  3a 20 66 6f 72 6d 2d 64  61 74 61 3b 20 6e 61 6d  |: form-data; nam|
00000170  65 3d 22 6b 22 0d 0a 0d  0a 70 57 6e 4a 79 47 59  |e="k"....pWnJyGY|
00000180  4f 55 35 67 49 4b 35 56  71 37 6f 61 5a 0d 0a 2d  |OU5gIK5Vq7oaZ..-|
00000190  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000001a0  2d 2d 2d 2d 2d 2d 2d 2d  2d 65 62 61 63 63 62 37  |---------ebaccb7|
000001b0  65 30 37 37 64 65 65 62  63 0d 0a 43 6f 6e 74 65  |e077deebc..Conte|
000001c0  6e 74 2d 44 69 73 70 6f  73 69 74 69 6f 6e 3a 20  |nt-Disposition: |
000001d0  66 6f 72 6d 2d 64 61 74  61 3b 20 6e 61 6d 65 3d  |form-data; name=|
000001e0  22 6d 22 0d 0a 0d 0a 59  6f 75 72 20 73 74 75 66  |"m"....Your stuf|
000001f0  66 20 69 73 20 62 65 69  6e 67 20 73 74 6f 6c 65  |f is being stole|
00000200  6e 21 0d 0a 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |n!..------------|
00000210  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 65 62  |--------------eb|
00000220  61 63 63 62 37 65 30 37  37 64 65 65 62 63 0d 0a  |accb7e077deebc..|
00000230  43 6f 6e 74 65 6e 74 2d  44 69 73 70 6f 73 69 74  |Content-Disposit|
00000240  69 6f 6e 3a 20 66 6f 72  6d 2d 64 61 74 61 3b 20  |ion: form-data; |
00000250  6e 61 6d 65 3d 22 73 22  0d 0a 0d 0a 73 6f 75 6e  |name="s"....soun|
00000260  64 0d 0a 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |d..-------------|
00000270  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 65 62 61  |-------------eba|
00000280  63 63 62 37 65 30 37 37  64 65 65 62 63 0d 0a 43  |ccb7e077deebc..C|
00000290  6f 6e 74 65 6e 74 2d 44  69 73 70 6f 73 69 74 69  |ontent-Dispositi|
000002a0  6f 6e 3a 20 66 6f 72 6d  2d 64 61 74 61 3b 20 6e  |on: form-data; n|
000002b0  61 6d 65 3d 22 76 22 0d  0a 0d 0a 76 69 62 72 61  |ame="v"....vibra|
000002c0  74 69 6f 6e 0d 0a 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |tion..----------|
000002d0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000002e0  65 62 61 63 63 62 37 65  30 37 37 64 65 65 62 63  |ebaccb7e077deebc|
000002f0  0d 0a 43 6f 6e 74 65 6e  74 2d 44 69 73 70 6f 73  |..Content-Dispos|
00000300  69 74 69 6f 6e 3a 20 66  6f 72 6d 2d 64 61 74 61  |ition: form-data|
00000310  3b 20 6e 61 6d 65 3d 22  69 22 0d 0a 0d 0a 69 63  |; name="i"....ic|
00000320  6f 6e 0d 0a 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |on..------------|
00000330  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 65 62  |--------------eb|
00000340  61 63 63 62 37 65 30 37  37 64 65 65 62 63 0d 0a  |accb7e077deebc..|
00000350  43 6f 6e 74 65 6e 74 2d  44 69 73 70 6f 73 69 74  |Content-Disposit|
00000360  69 6f 6e 3a 20 66 6f 72  6d 2d 64 61 74 61 3b 20  |ion: form-data; |
00000370  6e 61 6d 65 3d 22 63 22  0d 0a 0d 0a 69 63 6f 6e  |name="c"....icon|
00000380  63 6f 6c 6f 72 0d 0a 2d  2d 2d 2d 2d 2d 2d 2d 2d  |color..---------|
00000390  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000003a0  2d 65 62 61 63 63 62 37  65 30 37 37 64 65 65 62  |-ebaccb7e077deeb|
000003b0  63 0d 0a 43 6f 6e 74 65  6e 74 2d 44 69 73 70 6f  |c..Content-Dispo|
000003c0  73 69 74 69 6f 6e 3a 20  66 6f 72 6d 2d 64 61 74  |sition: form-dat|
000003d0  61 3b 20 6e 61 6d 65 3d  22 70 22 0d 0a 0d 0a 70  |a; name="p"....p|
000003e0  69 63 74 75 72 65 0d 0a  2d 2d 2d 2d 2d 2d 2d 2d  |icture..--------|
000003f0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
00000400  2d 2d 65 62 61 63 63 62  37 65 30 37 37 64 65 65  |--ebaccb7e077dee|
00000410  62 63 0d 0a 43 6f 6e 74  65 6e 74 2d 44 69 73 70  |bc..Content-Disp|
00000420  6f 73 69 74 69 6f 6e 3a  20 66 6f 72 6d 2d 64 61  |osition: form-da|
00000430  74 61 3b 20 6e 61 6d 65  3d 22 64 22 0d 0a 0d 0a  |ta; name="d"....|
00000440  32 35 32 35 38 0d 0a 2d  2d 2d 2d 2d 2d 2d 2d 2d  |25258..---------|
00000450  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
00000460  2d 65 62 61 63 63 62 37  65 30 37 37 64 65 65 62  |-ebaccb7e077deeb|
00000470  63 0d 0a 43 6f 6e 74 65  6e 74 2d 44 69 73 70 6f  |c..Content-Dispo|
00000480  73 69 74 69 6f 6e 3a 20  66 6f 72 6d 2d 64 61 74  |sition: form-dat|
00000490  61 3b 20 6e 61 6d 65 3d  22 75 22 0d 0a 0d 0a 75  |a; name="u"....u|
000004a0  72 6c 0d 0a 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |rl..------------|
000004b0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000004c0  65 62 61 63 63 62 37 65  30 37 37 64 65 65 62 63  |ebaccb7e077deebc|
000004d0  2d 2d 0d 0a                                       |--..|
000004d4
Again, rather than sift through this file, it might just be easier to generate and example of a working file that can be used with netcat. I think that could be a useful thing to have on this board anyway.

Or if you have any other suggestions I am open to ideas.

Just for your information I am using a spark fun board and could not find an example of interfacing with pushsafer using the FONA library they provide. This might be useful for others considering using spark fun with pushsafer int he future.

Thank you.

User avatar
admin
Site Admin
Posts: 943
Joined: 1. Dec 2014, 12:41
Location: Germany

Re: Posting with file using netcat (nc)

Postby admin » 20. Jul 2020, 19:14

try this one!
Add parameter you need in the same way and increase the Content-Length

Code: Select all

POST /api HTTP/1.1
Host: www.pushsafer.com
User-Agent: arduino 1.11
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 48

k=***************t=TITLE&m=MESSAGE&i=12&s=34&v=3
Pushsafer DE: Dir gefällt Service und Support! Dann freuen wir uns auf deine Bewertung!
Pushsafer EN: You like Service and Support! Then we look forward to your review!

Pushsafer.com Website - iOS - Android - Windows 10

nick9072

Re: Posting with file using netcat (nc)

Postby nick9072 » 21. Jul 2020, 16:41

Thank you. I made it work, but I had to change a couple of things.

Here is the file I ultimately used:

Code: Select all

POST /api HTTP/1.1
Host: www.pushsafer.com
User-Agent: arduino 1.11
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 76

k=********************&t=Important%20Mmessage:&m=two%20words&i=12&s=34&v=3


I think the example you gave was missing the "&" after the key, but it was a much simpler format to work with. Thank you.

They other important thing, it seems, is that the content length number (76 in this case) needs to include the line feed and carriage return in the total (or just don''t have a carriage return and line feed. So, in the example while it looks like there is only 74 characters, there are two more in the file that you can't see (and which might not have transferred in my copy and paste.)

This makes it very hard to copy a file from someone.

I made this simple awk script to more precisely generate the file. Note, you can remove the \r\n at the end of the command string and it will sill work, but it will just won't go to the next line at the end when you try to print it to a screen. (BTW, the key is not valid.)

Code: Select all

#awk -f script.awk > post.txt
BEGIN {

  cmd_str = "k=********************&t=Important%20Mmessage:&m=two%20words&i=12&s=34&v=3\r\n";

  printf("POST /api HTTP/1.1\r\n");
  printf("Host: www.pushsafer.com\r\n");
  printf("User-Agent: arduino 1.11\r\n");
  printf("Accept: */*\r\n");
  printf("Content-Type: application/x-www-form-urlencoded\r\n");
  printf("Content-Length: %d\r\n",length(cmd_str));
  printf("\r\n"); 
  printf("%s",cmd_str);
}
Thanks for your help.

User avatar
admin
Site Admin
Posts: 943
Joined: 1. Dec 2014, 12:41
Location: Germany

Re: Posting with file using netcat (nc)

Postby admin » 21. Jul 2020, 18:03

Yes, i forgot the "&" after the private key!

great to hear that it works now!

Please do not post your private key in your posts!!!
If you have further questions, please feel free to ask.
Pushsafer DE: Dir gefällt Service und Support! Dann freuen wir uns auf deine Bewertung!
Pushsafer EN: You like Service and Support! Then we look forward to your review!

Pushsafer.com Website - iOS - Android - Windows 10

nick9072

Re: Posting with file using netcat (nc)

Postby nick9072 » 21. Jul 2020, 20:03

I am just going to follow up for completeness.

I was able to interface with pushsafer using the fona.postData("GET", URL) call, where:

URL="www.pushsafer.com/api?k=*************** ... ge&d=99999";

I also tried using fona.HTTP_POST_start, but that gave me the following response from the server:
"status":0,"error":"invalid key or empty message"}
The key is fine so it looks like something is not getting through.

Here is the code for the HTTP_Post_start call.

Code: Select all

        
        
        uint16_t statuscode;
        int16_t length;
        char  url[60] = "www.pushsafer.com/api";
        char data[120] = "Content-Length: 74\r\n\r\nk=********************&t=Fona%20Message&m=Good%20Stuff&i=12&s=34&v=3";

        flushSerial();
        Serial.println(F("Posting to pushsafer.com:"));
  
        if (!fona.HTTP_POST_start(url, F("application/x-www-form-urlencoded"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
        //if (!fona.HTTP_POST_start(url, F("text/plain"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
          Serial.println("Failed!");
          break;
        }
It might be interesting to see the log files from these failed requests, but I guess for now I can just use the postData("GET",url) call.

Thanks again for your help.

User avatar
admin
Site Admin
Posts: 943
Joined: 1. Dec 2014, 12:41
Location: Germany

Re: Posting with file using netcat (nc)

Postby admin » 22. Jul 2020, 08:23

Sorry but i cant test this!
Pushsafer DE: Dir gefällt Service und Support! Dann freuen wir uns auf deine Bewertung!
Pushsafer EN: You like Service and Support! Then we look forward to your review!

Pushsafer.com Website - iOS - Android - Windows 10


Who is online

Users browsing this forum: Marinavaw and 28 guests