Client side setup to implement selector on a STOMP subscribe.
var headers = {hello: 'worldtest'};
console.log(event.get('header').data.eventType);
var connectCallback = function(frame) {
stompClient.subscribe("/topic/receipt", function(frame){console.log(frame);}, headers);
stompClient.send("/app/" + url.join('/'), {"content-type": "text/plain"}, message);
};
The subscribe returns a message logged to the console of the frame like:
body: ""<message>test</message>""
command: "MESSAGE"
headers: Object
content-length: "343"
content-type: "application/json;charset=UTF-8"
destination: "/topic/receipt"
hello: "world"
message-id: "4hw8wlab-1"
subscription: "sub-0"
Shouldn't the message have been filtered out due to the selector? Syntax of my selector?
Changing the syntax to the below did not work either. In this case, no message was returned at all.
var headers = {'selector': "hello = 'world'"};
What am I missing?
I can post my way to send selector inside header .. look:
Headers:{
"activemq.advisory" = true;
"activemq.prefetchSize" = 1;
selector = "switch = 'green'";
}
I'm using STOMP and Objective-C...
I hope that helps...
Related
The WebSocket server is a online testing one
The Website
Something goes wrong And I don't know how to fix it.
val client = HttpClient(CIO) { install(WebSockets) }
GlobalScope.launch {
client.webSocket("ws://82.157.123.54:9010/ajaxchattest") {}
}
the error printStackTrace
java.lang.IllegalStateException: Failed to parse request body: request body length
should be specified,
chunked transfer encoding should be used or
keep-alive should be disabled (connection: close)
not knowing how to enable encoding or disable keep-alive or specify body length.
The 82.157.123.54:9010/ajaxchattest endpoint responds with 403 Forbidden instead of 101 Switching Protocols if the Origin header is absent or invalid. So to make it work just append the Origin header with a well-formed value:
val client = HttpClient(CIO) { install(WebSockets) }
client.webSocket("ws://82.157.123.54:9010/ajaxchattest", request = {
header(HttpHeaders.Origin, "http://example")
}) {}
When my Telegram bot sends sendMessage to Telegram server it gets the error message:
{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
The problem appeared this morning, before that my bot worked a whole year without errors. GetUpdates command works well as before. I use GET HTTP method to send commads:
https://api.telegram.org/bot<MyToken>/sendMessage
with UTF-8-encoded data attached:
{"chat_id":123456789,"text":"any text"}
Has anyone encountered this?
If the issue still persists, try to modify your curl request. For me adding header
'Content-Type: application/json' and -d '{"chat_id":12309832,"text":"any text"}' fixed issue
Another way to send a message by emulating a form :
curl -s -X POST https://api.telegram.org/bot{apitoken}/sendMessage \
-F chat_id='-1234567890' -F text='test message'
Well, i wrote wrapper on C language to communicate via SSL with telegram bot api. SO now I can clearly answer questions about telegram API spec.
Problem number one
First of all if we are talking about raw queries we need to remember about specifications.
By default HTTP/HTTPS post requests should consists of:
<METHOD>[space]<PATH with only valid chars> <\r\n>
<HOST valid regexed\r\n>
<Content-type valid regexed><\r\n>
<Content-Length with length of your POST body data><\r\n>
<\r\n before body>
<body>
So, i tried to send raw queries with out Content-Length and i had error same as yours. That's the first problem.
Problem number two
By default if you trying to send non valid request with sendMessage method - telegram bot api will response with error same as yours. So, yeah, that's pretty tricky error to debug...
If you trying to send raw query, be sure that your JSON data is serialized nicely and there is no errors like shielding.
Summarizing
Request:
POST /bot<token>/sendMessage HTTP/1.1
Host: api.telegram.org:443
Connection: close
Content-Type: application/json
Content-Length: 36
{"chat_id":<integer>, "text":"test \\lol"}
Second backslash if shielding.
Code on C
sprintf(reqeustCtx.request,
"POST /bot%s/%s HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: close\r\n"
"Content-Type: application/json\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%s\r\n", bot_token, bot_method,
reqeustCtx.res_addr, strlen(body), body);
BIO_puts(bio, reqeustCtx.request);
BIO_flush(bio);
memset(reqeustCtx.response, '\0', BUFFSIZE);
read_bytes = BIO_read(bio, reqeustCtx.response, BUFFSIZE);
if (read_bytes <= 0) {
printf("No response");
exit(-1);
}
cert_free(cert_store, ssl_ctx, ca_cert_bio);
// free memory //
reqeustCtx.method(reqeustCtx.res_addr, reqeustCtx.request,
reqeustCtx.current_work_dir, reqeustCtx.current_cert);
/* json response, need to parse */
return reqeustCtx.response;
I got this error too.
I used sendMessage() method only with "low-level" Node https:
const https = require('https');
const data = JSON.stringify({
chat_id: config.telegram.chatId,
text: 'some ASCII text'),
});
const options = {
hostname: 'api.telegram.org',
port: 443,
path: `/bot${config.telegram.botToken}/sendMessage`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
let chunks = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => {
const resBody = Buffer.concat(chunks).toString('utf8');
if (res.statusCode === 200) {
console.log(`Message sent`);
} else {
console.error(`${res.statusCode} ${res.statusMessage} ${res.headers['content-type']}
${resBody}`)
}
});
});
req.on('error', (error) => {
reject(error)
});
req.write(data);
req.end();
And for ASCII text it was ok, however for some non-ASCII text I got:
const data = JSON.stringify({
chat_id: config.telegram.chatId,
text: 'Привет Мир!'),
});
Error:
400 Bad Request application/json
{"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
In my case content length was calculated with invalid length 'Content-Length': data.length (invalid for Telegram?...), so I comment out this header and now it works for UTF-8!
headers: {
'Content-Type': 'application/json',
//'Content-Length': data.length
}
In my case, I was using curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($fields)); to post this json via sendMessage method:
{
"chat_id":000000000,
"text":"Choose one of the following options: ",
"reply_to_message_id":292,
"reply_markup":{
"keyboard":[
[
"Enable",
"Disable"
]
]
}
}
The problem was that when passing fields to the curl_setopt method, I was encoding the whole php array so I solved it by just encoding the reply_markup array which was a part of my json.
Try to put "Message" object with chat_id & text to HttpEntity in your restTemplate service, like below:
public MessageDto sendMessage(Message message) {
return restTemeplate.exchange(
"https://api.telegram.org/bot{token}/sendMessage",
HttpMethod.POST,
new HttpEntity<>(message, HttpHeaders.EMPTY),
MessageDto.class
).getBody();
}
After updating to xcode8 Alamofire4, my AlamofireRequest is not working, returning " status code: 401, headers" (unauthorised) on the request. The user authorisation is correct (I have checked the site). I do not have any compiler errors, but note that 'headers' is not highlighted in blue as usual, so am thinking that it is not recognising the headers properly. Am I doing something wrong with the 'headers' here?
let user = "sampleUser"
let password = "samplepass"
let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
var checkUserEndpoint: String = "https://sample.com/ios1/user/\(uidEntered!).json"
print(checkUserEndpoint)
Alamofire.request(checkUserEndpoint, method: .get, parameters: nil, encoding: JSONEncoding.default, headers : headers)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.data)
I have already tried using this instead for the headers, but it made no difference:
var headers: HTTPHeaders = [:]
if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
also I tried this and it made no difference;
Alamofire.request(checkUserEndpoint,
method: .get,
parameters: nil,
encoding: JSONEncoding.default)
.authenticate(user: "sampleUser", password: "samplepass")
.validate()
.responseJSON { response in
print(response.request)
print(response.response)
print(response.data)
// print(response.error)
I've done similar migration twice, and my educated guess is that one of your strings you pass to generate headers value is Optional, ie. user, password or base64Credentials; though generating Optional("thestring") instead "thestring". You can try to wrap the request like this:
if let user = user, password = password, base64Credentials = base64Credentials {
let headers = ["Authorization": "Basic \(base64Credentials)"]
var checkUserEndpoint: String = "https://sample.com/ios1/user/\(uidEntered!).json"
print(checkUserEndpoint)
Alamofire.request(checkUserEndpoint, method: .get, parameters: nil, encoding: JSONEncoding.default, headers : headers)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.data)
}
This can happen eg. in a situation, where those values coming from the Objective-C code, where the variables are not marked nonnull.
... base64Credentials should not be optional though, as stated in the documentation.
The code is correct in both variants (manual header creation and Alamofire request .authenticate usage). Looks like server side issue, use curl/postman or any other REST client to receive success response from your server before continuing your app development.
I have an issue with AFNetworking and AFJSONRequestSerializer. I try to access an API, and the request contains a text/plain header. Here's my code :
class BaseService {
var manager: AFHTTPRequestOperationManager!
init() {
manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFJSONResponseSerializer()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions.allZeros)
}
}
class UserService: BaseService {
func startNewEntry(name: String) {
let params = [
"time_entry": [
"description": name,
"created_with": "fooBar"
]
]
manager.POST(
"endpoint",
parameters: params,
success: { (operation, response) -> Void in
let json = JSON(response)
println("OK")
println(json)
Context.shared.entries.getFromJSON(json)
}) { (operation, error) -> Void in
println("-- ERROR --")
println(operation)
println(error)
}
}
Do you know this issue ?
No, this code will create a request with a content type of application/json. But I wonder if you perhaps mislead by an error message that said:
Request failed: unacceptable content-type: text/html
If you got that, that's not telling you that that the request had an unacceptable content type, but rather that the request failed because the response was text/html. And this is a very common issue: If server code that is attempting to create a JSON response fails for some reason, sometimes the error message isn't JSON, but rather it's HTML.
I would suggest adding the following inside the failure block of your POST method in order to see what this text/html response was:
if operation.responseData != nil {
println(NSString(data: operation.responseData, encoding: NSUTF8StringEncoding))
}
This way, if you get a text error message from the server (e.g. the request was malformed or what have you), you'll be able to read the HTML response you got back.
I am generating custom Parse Analytic events in the background cloud code based on Save events from my ios app . I can also see it in the Analytic panel on Parse.com.
How do I access it in my ios app ?
I get the following error when I try
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7a9697e0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7a956d00> { URL: https://api.parse.com/1/events/commentsAnalytics } { status code: 400, headers {
"Access-Control-Allow-Methods" = "*";
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 36;
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 23 Jan 2015 06:43:38 GMT";
Server = "nginx/1.6.0";
"X-Parse-Platform" = G1;
"X-Runtime" = "0.007086";
} }, NSErrorFailingURLKey=https://api.parse.com/1/events/commentsAnalytics, NSLocalizedDescription=Request failed: bad request (400),
Check if you are adding the keys (X-Parse-Application-Id and X-Parse-REST-API-Key) of your application in the request header. Here is an example with Alamofire.
Example
var request = NSMutableURLRequest(URL: NSURL(string: "https://api.parse.com/1/events/Buy")!)
request.HTTPMethod = "POST"
request.setValue("<APPLICATION-KEY>", forHTTPHeaderField: "X-Parse-Application-Id")
request.setValue("<REST-KEY>", forHTTPHeaderField: "X-Parse-REST-API-Key")
var parameter: NSDictionary = ["dimensions" :["product" : ["name" : "macpro", "price" : "350"]]]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameter, options: nil, error: nil)
Alamofire.request(request).response { (request, response, result, error) -> Void in
// handle response
println("\(request) \t \(response) \t \(result) \t \(error) ")
}
Checking
to check the event, go to your application panel in the parse ...
...click "Custom Breakdown"...
and customize your chart.