Http_post/3 - How do I avoid sending current cookies during a request?

I need help using the http_post/3 in SWI 8.3.24

I am talking to a httpd server who didnt seem to acct for multiple post variables

wget --post-data 'The quick brown fox jumped over the lazy dog.' 'localhost:9000/?properties={"annotators":"tokenize,ssplit,pos","outputFormat":"json"}' -O -

So need to do the following:

?- http_post(
 'http://localhost:9000/?properties={%22annotators%22%3A%22tokenize%2Cssplit%2Cpos%22%2C%22outputFormat%22%3A%22json%22}',
   ['The quick brown fox jumped over the lazy dog.'], json(Json),   []).

But I seem to be sending extra POST data that seems related to the Date/Cookies

Like multipart/mixed; boundary=------1621284001.759324e4ec0d21aa6d3aee3"

How do I not send that extra POST data ?

-Douglas

I don’t really get it. You want to submit multiple pieces of data without creating a multipart message? I guess the simplest way is to first combine the data into a single document (e.g., string) and post that?

No, what I need to do is find a way prevent SWI Prolog from automatically converting my non-multipart requests into multipart requests (It seems to be doing this weird behaviour in order to create non-existent cookies)

My workaround (that works) shows what I am trying to do is this simple:

get_post_reply(_Host,URL, PostData, Reply):-
  atom_length(PostData,Len),
  tcp_socket(Socket),
  tcp_connect(Socket, 'localhost':4090),
	tcp_open_socket(Socket, Read, Write),
  sformat(S,"POST ~w HTTP/1.0\r
From: logicmoo@gmail.com\r
User-Agent: Raw-Socket/1.0\r
Content-Type: application/x-www-form-urlencoded\r
Content-Length: ~d\n\n~w\n",[URL,Len,PostData]),
  format(Write,"~s",[S]), flush_output(Write),catch(close(Write),_,true),
  % format(user_error,"~N`~s`~N",[S]), flush_output(user_error),
  peek_code(Read,C),  
  ( [A123] = `{` ),
   my_stream_to_codes(A123,C,Read,_,[]),
   my_stream_to_codes(-1,A123,Read,Codes,[]),
  %format(user_error,"~N~s~N",[Codes]), flush_output(user_error), 
   catch(close(Read),_,true), 
   open_codes_stream(Codes,Stream),
   json:json_read(Stream,Reply),
   close(Stream),
  !.

my_stream_to_codes(_,-1, _Stream, Tail, Tail) :- !.
my_stream_to_codes(EOC,EOC, _Stream, Tail, Tail) :- !.
my_stream_to_codes(EOC,C, Read, [C|T], Tail) :- 
  % format(user_error,"~s",[[C]]), flush_output(user_error), 
   get_code(Read,C), 
   peek_code(Read,C2),   
   my_stream_to_codes(EOC,C2, Read, T, Tail).

( logicmoo_workspace/parser_stanford.pl at 895fd4c26ad6ea977d01cd9f89eaf4ca7a5d93ba · logicmoo/logicmoo_workspace · GitHub )

Ideally it would be better to use the http library to achieve this! Such as http_post/4 should be capable of doing what I am doing above?

get_post_reply(HOST,URL, PostData, Reply):- 
   atom_concat(HOST,URL,PARAMS), 
   http_post(PARAMS, [PostData], json(Reply), []). 

OK I looked at http_post_data/3 a little more closely and realized I needed to

get_post_reply(HOST,URL, PostData, Reply):- 
   atom_concat(HOST,URL,PARAMS), 
  atom_codes(PostData,Codes),
   http_post(PARAMS, [codes('application/x-www-form-urlencoded', Codes)], json(Reply), []).

Socket works but not this Http post

OK, removed the List brackets and now it works…


get_post_reply(HOST,URL, PostData, Reply):- 
   atom_concat(HOST,URL,PARAMS), 
   http_post(PARAMS, % atom works for both Strings and Atoms
     atom('application/x-www-form-urlencoded', PostData), 
      json(Reply), []),!.

Thank you!

1 Like