SWI-Prolog read http header
Asked Answered
A

1

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).
Aldric answered 10/6, 2017 at 18:9 Comment(0)
F
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.

Figural answered 10/6, 2017 at 20:44 Comment(3)
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.Aldric
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.Figural
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!Aldric

© 2022 - 2024 — McMap. All rights reserved.