来自javascript的HTTP请求使用包括标头的原始消息 [英] HTTP request from javascript using raw message including headers

查看:339
本文介绍了来自javascript的HTTP请求使用包括标头的原始消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用jQuery或XMLHttpRequest从javascript向我的REST api发出HTTP请求。我现在要做的是在不设置标题值属性的情况下发出请求。 HTTP请求消息包含:

I know how to make an HTTP request to my REST api from javascript using jQuery or XMLHttpRequest. What I want to do now is make the request without setting properties for the header values. An HTTP request message consists of:



  • 请求行,例如GET /images/logo.png HTTP / 1.1,它从服务器请求名为/images/logo.png的资源

  • 标题,例如Accept-Language:zh

  • 空行

  • 可选消息正文

  • Request line, such as GET /images/logo.png HTTP/1.1, which requests a resource called /images/logo.png from server
  • Headers, such as Accept-Language: en
  • An empty line
  • An optional message body

对我的api的请求应如下所示:

A request to my api should look something like this:

GET /myapi/myresource/1234 HTTP/1.1
Host: localhost:51127
Content-Type: application/x-www-form-urlencoded
Accept: application/json, text/csv
Authorization: Basic <base64 encoded credentials>

我想打开一个到localhost的连接:51127,发送上面的文字,并得到回复。这可以在javascript中使用吗?

I would like to open a connection to localhost:51127, send the above text, and get the response. Is this possible in javascript?


更新:我知道如何设置标题。我只想以不同的方式做到这一点。
有很多方法可以构建
请求,包括标题并发送它。
我只是想手动构建它。

Update: I know how to set headers. I just want to do it a different way. There are lots of ways to "build" a request including headers and send it. I just want to build it manually.


推荐答案

没有套接字支持在Javascript中。您只能使用 XMLHTTPRequest 包装器或 的包装器来构建HTTP查询,例如 jQuery.ajax 。这是出于各种好的理由,主要是安全性。

There is no socket support in Javascript. You can only build HTTP queries by using the XMLHTTPRequest wrapper, or optionally wrappers for that such as jQuery.ajax. This is for all kinds of good reasons, principally security.

这篇关于来自javascript的HTTP请求使用包括标头的原始消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆