AyoubElk/grammarbot

CORS

Closed this issue · 8 comments

I cannot return a response from the GrammarBot API (either using this library or a standard Ajax request) from a client in the browser due to CORS errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://api.grammarbot.io/v2/check?api_key=AF5B9M2X&text=This%is%a%20tst.. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

This is the error I'm getting when clicking that url:
image
And it has to do with the text provided, it isn't correctly formated:

  • A url encoded space character is %20 not %

image

If you're really facing a CORS issue like you mentioned, can you share more details?

Sorry, I had formatting errors in my example. When trying "http://api.grammarbot.io/v2/check?api_key=AF5B9M2X&text=This%20is%20a%20tst" through CURL everything looks fine.

I tried implementing the Grammarbot library as well but I am still receiving text formatting errors. Is it possible to pass in an unformatted string as a parameter in the bot.check() method? Should I remove/replace white spaces first? The example provided in the documentation seems to suggest that a plain string would work.

Yes a plain string should work, please provide a code sample and its output so I can debug the problem with you.

Here is the server function implementation I am working on:

const { send } = require('micro')
const cors = require('micro-cors')()

const Grammarbot = require('grammarbot')
const key = process.env.GRAMMARBOT_KEY

const bot = new Grammarbot({
	'api_key': key,
	'language': 'en-US',
  	'base_uri': 'api.grammarbot.io'
})

const handler = (req, res) => {
	if (req.method === 'OPTIONS') {
		return send(res, 200, 'ok!')
	}


	if (req.method !== 'POST') {
		throw createError(404, 'Not Found')
	}

	console.log(req.body.textContent)

	bot.check(req.body.textContent, function(error, result) {
		if (error) {
			result = error
			throw createError(402, 'Error')
		} else {
			result = JSON.stringify(result)
			return send(res, 200, result)
		}
	})

	
}

module.exports = cors(handler)

This is the output returned after sending a post request with a text string as the "textContent":

{"timestamp":"2019-07-18T04:11:11.787+0000","status":400,"error":"Bad Request","message":"Required String parameter 'text' is not present","path":"/v2/check"}

Thanks @thaddeusm
Looks like there is something changed at the API level, the example in the documentation doesn't work anymore as well.
Will check later during the day.

@AyoubElk Thank you for looking into this. I am looking forward to using this API.

I've been trying to use this today and I'm getting the same issue.

I just pushed a fix.

I'll close this issue, feel free to re-open it if needed.