6

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).
Will Ness
  • 70,110
  • 9
  • 98
  • 181
user27815
  • 4,767
  • 14
  • 28

1 Answers1

4

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.

mat
  • 40,498
  • 3
  • 51
  • 78
  • 2
    Thank you that explained what I was confused about. I was trying to cutout code that I thought would get in the way of what I was asking. – user27815 Jun 10 '17 at 21:07
  • 2
    I understand that. But it is too much to expect that people who want to answer the question also come up with matching client code that fits your concrete use case. Please include a client that makes clear what you are doing in such cases. To cut down the code, you can use a `wget` or `curl` sample invocation to produce a fitting client request. There are several such questions already that could have been answered easily if the use case were described more fully. – mat Jun 10 '17 at 21:10
  • 2
    The client code is not under my control in this case so I could not give it to you. The system gets connected to by another service, and im trying to work out what it is sending so that was the problem! – user27815 Jun 10 '17 at 21:52