我正在尝试使用$ http服务连接到API,该服务需要为POST请求定义某个标头:
app.run(['$http', function($http) {
$http.defaults.headers.post.hello = 'world';
}]);
Run Code Online (Sandbox Code Playgroud)
但是,我的Angular应用程序OPTIONS在之前发送请求POST,并且它似乎没有发送自定义标头.
退房的网络标签在此Plunker:http://plnkr.co/edit/tcqIb2q9zIQb3nf2SzyT
并且观察请求应该在哪里发送hello带有值的标头world,它实际上不是:

我看到hello的access-control-request-header,但不应该把它放在自己的路线?为什么不发送价值world呢?
我创建了一个REST Web服务,它返回一个以xml格式传递的id.
但是,作为返回的xml的一部分,我想要包含自定义标头.我希望能够读取请求标头中的数据,并在响应中返回其中一些数据.
例如,如果请求包含Header1和Header2,我想返回Header1和Header2作为响应的一部分,以及一个新的头文件Header3.
我正在努力找出在哪里以及如何做到这一点,所以任何帮助将不胜感激.
我的代码:
RestServiceImpl.svc.cs:
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region iRestService Members
public string XMLData(string id)
{
return "You requested product " + id;
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
IRestServiceImpl.cs:
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
}
}
Run Code Online (Sandbox Code Playgroud) 在从URL中取消实现json时,设置api键值的最佳方法是什么.我正在使用的代码如下,但我不知道如何陈述api密钥
using (var webClient = new System.Net.WebClient())
{
var myTable = webClient.DownloadString(url);
var deserealizedTable = JsonConvert.DeserializeObject<MyClass>(myTable);
}
Run Code Online (Sandbox Code Playgroud)
密钥的提供者说我应该修改我的客户端以使用标题字段"Authentication-Token"和作为值提供的标记.
我创建了这个REST映射,以便它可以接受URI末尾的文件名...
@RequestMapping(value="/effectrequest/{name}/{imagename:[a-zA-Z0-9%\\.]*}",
headers="Accept=*/*", method=RequestMethod.GET,
produces = "application/json")
public @ResponseBody EffectRequest effectRequest(
@PathVariable("name") String name,
@PathVariable("imagename") String imageName)
{
return new EffectRequest(2, "result");
}
Run Code Online (Sandbox Code Playgroud)
它使用MappingJackson2HttpMessageConverter返回JSON内容.我用这个映射测试jQuery AJAX调用...
var effectName = 'Blur';
var imageName = 'Blah.jpg';
var requestUri = '/effectrequest/' + effectName + '/' + imageName;
alert(requestUri);
$(document).ready(function() {
$.ajax({
url: /*[+ [[${hostname}]] + requestUri +]*/
}).then(function(data) {
$('.effect').append(data.id);
$('.image').append(data.content);
});
});
Run Code Online (Sandbox Code Playgroud)
这将生成一个http://localhost/effectrequest/Blur/Blah.jpg的URI,并在调试会话中,在上面的effectRequest()方法中正确接收文件名.但是,客户端或jQuery AJAX调用从服务器收到HTTP 406错误(不可接受),即使produces = "application/json"在RequestMapping中也是如此.
经过多次调试后,我对此进行了缩小 - 当我修改测试javascript代码以生成http://localhost/effectrequest/Blur/Blah.json其工作的URI时.因此要么是Tomcat,要么MappingJackson2HttpMessageConverter是通过查看URI末尾的文件扩展名并确定我发回的JSON内容不是很好的匹配而导致HTTP 406错误.
无论如何都要覆盖此行为而不必编码.(点)在文件名中?
尝试使用ajax在Phalcon中调用DELETE或PUT时,我收到404错误.我能够做一个GET.
我的阿贾克斯:
$.ajax({
url: 'http://localhost/person/blah/5',
type: 'DELETE',
success: function (data) {
console.log(data);
}
});
Run Code Online (Sandbox Code Playgroud)
我的PHP
$app->delete('/person/blah/{id:[0-9]+}', function($id) {
$response = new Phalcon\Http\Response();
$response->setJsonContent(array('status' => 'OK', 'data' => array('id' => $id)));
return $response;
});
Run Code Online (Sandbox Code Playgroud)
在此之前,我遇到了DELETE,PUT和GET的CORS问题.我更改了我的.htaccess文件以允许访问控制,并且GET开始工作.但是,我现在有404问题DELETE和PUT.
这是我的.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "DELETE, PUT, GET"
Header set Access-Control-Allow-Headers: "X-Requested-With, Content-Type"
</IfModule>
Run Code Online (Sandbox Code Playgroud)
我的猜测是这个问题与CORS有关.javascript正在我的计算机上运行(而非服务器).我的javascript在C:/Users/username/Desktop/test.html中.我曾在Firefox和Chrome上试过并遇到同样的问题.
一些额外的信息
响应标题
Access-Control-Allow-Head... X-Requested-With, Content-Type
Access-Control-Allow-Meth... DELETE, PUT, GET
Connection Keep-Alive
Content-Length 15
Content-Type …Run Code Online (Sandbox Code Playgroud) 我想基于Accept HTTP标头路由到其他Flask视图,例如:
@api.route('/test', accept='text/html')
def test_html():
return "<html><body>Test</body></html>"
@api.route('/test', accept='text/json')
def test_json():
return jsonify(test="Test")
Run Code Online (Sandbox Code Playgroud)
我没有在Flask使用的Werkzeug Rule构造函数中找到相关选项。它是缺少的功能还是有可能以不同的方式实现相同的效果,例如通过在路由之前拦截和修改URL路径?
我不想将视图合并为一个视图,因为这将使代码显着复杂化,其中有许多视图位于不同的蓝图中。
我知道有人问过类似的问题,但没人用Flask回答。可以在不同的Web框架中执行此操作,例如在Pyramid中使用谓词 -可以在此答案中找到示例代码。
问题:Vary当服务器接受gzip/deflate连接并为移动客户端提供不同的内容时,发送HTTP标头的最佳方法是什么?
我看到两种可能的变体,但我找不到任何有用的信息,无论它们是否正确和/或大多数代理和搜索引擎支持:
Vary: Accept-Encoding,User-AgentVary: Accept-Encoding
Vary: User-Agent
欢迎任何信息或链接到适当的W3C标准:)
(怎么样)我能否认为scrapy只下载网站的标题数据(用于检查目的等)
我试图禁用一些下载中间件但它似乎不起作用.
是否可以在JAXRS资源方法中检索http标头而无需将这些标头明确指定为方法参数?
例如,我有一个以下界面:
@Path("/posts")
public interface PostsResource {
@GET
public List<Post> getAllPosts();
}
Run Code Online (Sandbox Code Playgroud)
以及实现此接口的以下类:
public class PostsResourceImpl implements PostsResource {
@Autowired
private PostsService postsService;
public List<Post> getAllPosts() {
return postsService.getAllPosts();
}
}
Run Code Online (Sandbox Code Playgroud)
我不想将我的方法签名更改为:
public List<Post> getAllPosts(@HeaderParam("X-MyCustomHeader") String myCustomHeader);
Run Code Online (Sandbox Code Playgroud)
此标头将由客户端上的拦截器添加,因此客户端代码不知道放在此处的内容,这不应该是显式方法参数.
我需要向仅包含"主机"和"内容长度"标头的网络服务器发送帖子请求.我在字典中指定了这两个标题,我将其传递给请求模块,但它添加了"接受","接受编码","用户代理"标题.
Python代码:
headers = {'Content-Length': content_length, 'Host': 'Server-1:8080'}
r = requests.post(url, data=data, headers=headers)
print(r.request.headers)
Run Code Online (Sandbox Code Playgroud)
发送的实际请求标头:
{'Accept': '*/*', 'Host': 'Server-1:8080', 'Content-Length': '3072', 'User-Agent': 'python-requests/2.6.0 CPython/3.4.1 Windows/7', 'Connection': 'keep-alive, 'Accept-Encoding': 'gzip, deflate'}
Run Code Online (Sandbox Code Playgroud)
如何限制请求模块发送的标头?