我正在尝试通过序言向其他rest api发出发布请求。 (如果我要在js中执行):
body={
login="login",
passsword="password"
}
axios.post("http://localhost:5000",body);
我不是要发布TO序言,而是要将FROM序言发布到另一个api。
我不知道如何将正文添加为json并将Content-Type设置为application / json。
这是我到目前为止提出的:
% Bibliotecas
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/http_ssl_plugin)).
:- use_module(library(http/http_open)).
:- use_module(library(http/http_json)).
% Routes
:- http_handler('/auth', auth, []).
:- http_handler('/send_file_post', send_file_post, []).
% Cria��o de servidor HTTP no porto 'Port'
server(Port) :-
http_server(http_dispatch, [port(Port)]).
auth(Request) :-
http_read_json(Request, DictIn,[json_object(dict)]),
format(user_output,"Request is: ~p~n",[Request]),
format(user_output,"DictIn is: ~p~n",[DictIn]),
DictOut=DictIn,
reply_json(DictOut),
http_client:http_post('http://localhost:5000/api/users/authenticate', DictOut,Reply, [content("application/json")]),
http_read_data(Reply, Data, []).
要进一步说明,请执行以下操作:
我向我的prolog http服务器发出了发布请求,在它的正文中,我有一封电子邮件和一个密码。我的最终目标是使用相同的电子邮件/密码向另一个api发出第二个发布请求。
有什么建议吗?
答案 0 :(得分:0)
您可以使用http_post/4
。
https://www.swi-prolog.org/pldoc/doc_for?object=http_post/4
http_post([ protocol(http),
host(localhost),
port(5000),
path('/mypostpage')
],
form_data([ login = myusername,
password = pass123
]),
Reply,
[]).
将发帖请求发送到http://localhost:5000/mypostpage