Is it possible to encode a C char array (char*) in JSON in order to send it through a TCP socket? If so how can I make it and which would be a simple JSON library to use?
Thanks,
You probably don't need to use JSON. If you just want to send a simple message (e.g. "Hi, how are ya"), then you can just send the char array. You cannot send JSON, only a char array of JSON (JSON is a data format, not a transfer protocol).
Example
const char *json = "{\"id\": 12837, \"name\": \"Massimo Isonni\"}"
// You would then send json.
If you need to serialize a struct into a JSON string, I would recommend this.
Related
char get_buffer[10000];
SSL_write(conn,https_request_get,strlen(https_request_get));
printf("GET Sent...\n");
byte_count = SSL_read(conn,get_buffer,sizeof(get_buffer));
printf("recv()'d %d bytes of data in get_buff\n",byte_count);
printf("%s",get_buffer);
fprintf(html, "%s",get_buffer);
fwrite(get_buffer,sizeof(get_buffer),1,html); //html is the file pointer
I've written a C program with sockets that "downloads" the landing page HTML of a given website from the HTTP response. I was able to store the entire HTTP response in a char array and was also able to print it in the console using printf("%s",buffer_name).
Now I am trying to write the same array into a file ( fprintf()), but it only prints the HTTP response excluding the HTML of the page. I understand that there can be null characters and I also tried
fwrite(buffer,sizeof(buffer),1,file_ptr)
which gave me the same output as before (didn't put HTML into the file).
Can anyone help me out with this ?
If I use the standard library to make this response, the type Writer Interface only takes a slice of bytes as an argument, but an array wrapped JSON is [][]byte
I know there must be something I'm missing here...
And as a note, I tried using the Echo Framework for this and it does successfully send but it's auto encoding the contents into base64 which is not what I want for my API right now.
json, err := json.Marshal(j)
check(err)
arrJSON := [][]byte {
json,
}
fmt.Printf("arrJSON: %s", arrJSON) // this prints correctly
return c.JSONPretty(http.StatusOK, arrJSON, "") //this returns a base64 encoded array wrapped JSON (it works but I wan't decoded)
Hello I've got a problem trying to create a twitter console app. When getting a response I've got some thrash in it.
Here my code is:
char new_request[1024] = "GET /1.1/statuses/user_timeline.json?count=4&screen_name=twitterapi HTTP/1.1\r\nHost: api.twitter.com\r\nUser-Agent: twitter-terminal-app$
strcat(new_request, bearer);
strcat(new_request, "\r\nAccept-Encoding: gzip\r\n\r\n\0");
BIO_write(bio, new_request, strlen(new_request));
printf("%s\n", new_request);
p = BIO_read(bio, ans, 2047); // Getting header
ans[p] = 0;
printf("%s\n", ans);
char ans2[100000] = "";
p = BIO_read(bio, ans2, 10000); // Getting body
BIO_should_retry(bio);
FILE *file = fopen("result.txt", "w+");
fputs(ans2, file);
printf("%s\n%i\n", ans2, p);
The answer I have in body looks like that:
▒▒is▒HǿJ▒_
▒▒▒▒V▒▒▒▒.▒▒T▒▒f
▒Tm▒m
▒P▒▒1▒|▒}▒%▒▒▒▒Q^▒▒▒▒?▒cx{Չ
F%▒C*;▒▒ŴD/#▒L▒▒▒▒?L▒A▒▒▒N▒▒ĝ▒▒▒▒$▒El▒▒▒▒X▒▒B0▒~%▒▒5▒˲#Y)GY▒ctz▒h&-▒▒▒O>AG▒▒▒l6b▒z▒:K
▒T▒\▒▒2+▒b▒H▒&B ▒▒▒B▒qV▒▒▒▒▒▒
▒▒a_▒l▒?#▒o▒▒▒W▒▒u%▒▒▒;▒1M▒v▒▒L▒▒
Maybe the answer is encoded somehow and that's the problem. Tried to serf dev.twitter.com but didn't find any answer. If I use BIO_gets() instead of BIO_read() the answer is -2. Any ideas?
strcat(new_request, "\r\nAccept-Encoding: gzip\r\n\r\n\0");
...
p = BIO_read(bio, ans, 2047); // Getting header
...
p = BIO_read(bio, ans2, 10000); // Getting body
... Maybe the answer is encoded somehow
You've declared in your HTTP request that you support data compressed with gzip and that's why the server has send you the data compressed. If you would not only read and ignore the HTTP response header but actually take a look at it you would probably notice:
Content-Encoding: gzip
Apart from explicitly allowing compressed data you are doing a HTTP/1.1 request. This means that you would also need to be able to deal with chunked transfer encoding. HTTP/1.1 also implies that by default that connections are persistent so you would need to properly parse the header to find out where the response really ends instead of relying on connection end. You also cannot rely on a fixed size of the header or that header and body can be read with separate BIO_read calls. For example you might need multiple reads for the body or the body might already be included in the single read you do for the header.
Unless you really want to deal with all these problems yourself I recommend you better use an existing library which implements this properly and thus gets you the correct response reliably and not by chance.
If you instead want to learn how this is done I recommend you start with learning more about HTTP, i.e. by reading the wikipedia entry and then continue with all the standards referenced there. I suggest to start with HTTP/1.0 since this is simpler than HTTP/1.1.
The problem
1- I'd like to create a MIME message. Something like this:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="---12345"
This is a multipart message in MIME format.
---12345
Content-Type: text/plain
This is the plain text
---12345
Content-Type: application/pdf
>> PDF binary code here <<
---12345
2- Then i need to pass it to the OpenSSL functions in the form of BIO * data.
PKCS7 *PKCS7_sign(..., ..., ..., BIO *data, ...);
The first approach
Load the plain text and attachment data from the filesystem and assign it to char * data, manipulate the data to add the respective MIME headers, finally assign it to BIO * memoryBIO with BIO_puts(memoryBIO, data);.
But this approach doesnt work because the binary data contains "\0"(NULL) which wont go well with char type.
The second approach
Assign plain text and attachment to a BIO each and then "concatenate" them. But couldn't find a way to do this.
Conclusion
I'd like to know if there is a way to accomplish such feat.
I'd also like to avoid using intermediary files and build everything in-memory.
After following jww's suggestion, what i ended up using is BIO_write();.
BIO * inBIO = NULL;
std::vector<unsigned char> data = GetData();
inBIO = BIO_new(BIO_s_mem());
BIO_write(inBIO, data.data(), data.size());
Again thanks to jww for this answer and for all the other answers regarding OpenSSL in SO, you've helped me a great deal.
I am trying to read XML data in Arduino from a google spreadsheet published to the web using an HTTP GET request to the following link.
https://spreadsheets.google.com/feeds/cells/SpreadsheetID/5/public/basic?&range=D10
I receive the following reply along with some headers which I can observe on the serial port.
I want to parse the data written in bold format in the above reply. The data can be a real number and can be positive & negative. Please help me to find a way to parse this data.
if you need only one-two easy(out of param) nodes, you can use my function :)
String xmlTakeParam(String inStr,String needParam)
{
if(inStr.indexOf("<"+needParam+">")>0){
int CountChar=needParam.length();
int indexStart=inStr.indexOf("<"+needParam+">");
int indexStop= inStr.indexOf("</"+needParam+">");
return inStr.substring(indexStart+CountChar+2, indexStop);
}
return "not found";
}
I tried to find a solution for a long time, leave it here for clarity.
The complex xml with repeating nodes already so will not succeed.
Have you checked out this library?
https://web.archive.org/web/20160622041818/http://interactive-matter.eu/how-to/ajson-arduino-json-library/
You're better off converting your XML to JSON and giving that a go considering the memory availability on Arduino.
Otherwise if you really want to work with XML, then there's always these resources:
https://github.com/RobTillaart/Arduino/tree/master/libraries/XMLWriter
http://john.crouchley.com/blog/archives/454