Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWI-Prolog read http header

I don't fully understand how SWI Prolog handles http. I have the following code which mostly works apart from the get_header/1. I need to be able to reader the header file of the http request to get a value. How do I do that? Do I use http_read_header/2 ? If so how?

:- http_handler(root(handle), myhandle,[]).

myhandle(Request):-
  get_header(H),
  http_read_json_dict(Request,DictIn),
  handle_dict(DictIn,DictOut),
  reply_json(DictOut).

get_header(H):-
  http_read_header(current_input, H),
  something(H).
like image 913
user27815 Avatar asked Jun 10 '17 18:06

user27815


1 Answers

First, when posting a question about the HTTP libraries, please include the full code.

This means the server and client that you use to post the request.

From just your question, nobody has any idea what you are doing. This is typical for questions about HTTP libraries, and I hope becomes less common in the future.

Second, the Request is already a list of Name(Value) elements.

Any header field that was sent by the client is included in this list. It is simply a matter of looking up the value in this list, using typical predicates that reason over lists, such as member/2 and option/3.

For example, if the client has submitted the header The-Field: x, then

member(the_field(Value), Request),
...

will yield Value = x.

like image 139
mat Avatar answered Oct 04 '22 02:10

mat