Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Which is the syntax to post octet-stream and JSON? #78

Open
FabioBentoLuiz opened this issue Nov 8, 2019 · 2 comments
Open

Which is the syntax to post octet-stream and JSON? #78

FabioBentoLuiz opened this issue Nov 8, 2019 · 2 comments
Labels

Comments

@FabioBentoLuiz
Copy link

Hi,
I'm trying to implement two HTTP POSTs: one containing some file data (application/octet-stream) and another posting a simple JSON (application/json).
Anyone could provide a sample?

@boazsegev
Copy link
Owner

I'm assuming you're trying to implement client POST requests...?

Are you sending them one after the other (on the same connection)? Are they sent on different connections?

What did you try? where are you stuck?

There's a simple client example in the examples folder.

There's the comment in the example that said first response is always empty, nothing was sent yet... right before the http_finish would be where you would want to set any headers.

If you want to send a second request, set new headers and send another request... just make sure to somehow be able to test for the response cycle (you could simple change the callback used to rotate callbacks according to state).

@FabioBentoLuiz
Copy link
Author

Hi, thank you for the fast answer.

I'd like to focus first in a simple client POST with JSON. I believe that when I get it working I'll manage to use octet-stream. The json structure is as simple as:

{"userId":"1", "message":"test"}

What did you try? where are you stuck?

I've sucessfully implemented a client GET as follows:

sprintf(header, "%s:%s/getFingerprint?userId=%d", ip, port, userId);
http_connect(header, NULL, .on_response = on_http_response);

Then I read the response on on_http_response . It was based in your samples and the documentation.

static void on_http_response(http_s *h) {
  if(h->status_str == FIOBJ_INVALID){
	  //ignore the first empty response
	  http_finish(h);
	  return;
  }
  
  http_parse_body(h);
  FIOBJ obj = h->body;
  fio_str_info_s raw = fiobj_obj2cstr(obj);
  fprintf(stdout, "Fingerprint received - size = %d\n", raw.len);
  
  data = fp_print_data_from_data((unsigned char *)raw.data, raw.len);

  fiobj_free(obj);
 
 //continue with some processing...
}

For the aforementioned client GET, the parameter is already set on http_connect, so I was wondering if for a client POST it would be similar (maybe build a FIOBJ and set it on http_connect...).

If I understood you well, your suggestion ins that I should call http_connect without any parameter, then somehow set the POST json parameter from inside on_http_response (before http_finish), that is correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants